Python_A9
Python_A9
You need to prepare for the programming mission. Follow the instructions to write code that
starts and stops loops. This is an important skill. Plus, it is a great chance to be methodical.
A while loop can repeat instructions forever. It is used when you do not know how many times
the instructions should repeat. A variable can be used to start and stop it.
To stop the loop, press CTRL + C on the keyboard. Close the Python Shell.
Count Forever
count=0
while loop:
print('I like to count.')
To stop the loop, press CTRL + C on the keyboard. Close the Python Shell.
To stop the loop, press CTRL + C on the keyboard. Close the Python Shell.
Things are happening too fast. Slow things down to see what is happening.
6. Import the Time library and add a delay before the loop repeats:
import time
loop=True
count=0
print('I like to count.')
while loop:
count=count+1
print(count)
time.sleep(1)
Run the program again to see the change. Close the Python Shell.
A while loop runs until a condition is met. To stop it, the variable that controls the loop must
change to False.
7. Stop the loop when count equals 5:
while loop:
count=count+1
print(count)
time.sleep(1)
A while loop is used when you do not know how many times the instructions should repeat. Have
the player decide if they want to stop counting.
8. Ask the player if they want to stop:
if count==5:
stop=input('Do you want to keep counting? yes or no? ')
if stop=='no':
indent loop=False
4 spaces print('I will stop now.')