#!/usr/bin/env python # coding: utf-8 # # Continue And Break Loops # - **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon) # - **Date:** - # - **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py) # - **Note:** # ### Import the random module # In[4]: import random # In this while loop: # - If the number is less than 3, the loop restarts again. # - If the number is 4, it changes running to false and the loop ends. # - If the number is 5, it breaks out of the loop and the loop ends. # ### Create a while loop # In[7]: # set running to true running = True # In[8]: # while running is true while running: # Create a random integer between 0 and 5 s = random.randint(0,5) # If the integer is less than 3 if s < 3: # Print this print('It is too small, starting over.') # Reset the next interation of the loop # (i.e skip everything below and restart from the top) continue # If the integer is 4 if s == 4: running = False # Print this print('It is 4! Changing running to false') # If the integer is 5, if s == 5: # Print this print('It is 5! Breaking Loop!') # then stop the loop break