A while loop keeps running as long as its condition stays True.
If you forget to change the condition inside the loop, it will run forever.
number = 10
while number >= 0: # loop stops when number is -1
print(number)
number -= 1 # move toward the stopping point
print("Blast-off!")- The loop prints
10 9 8 … 0. - Updating
number(number -= 1) is crucial; otherwise the testnumber >= 0would never becomeFalse.
command = ""
while command.lower() != "quit":
command = input("> ")
print("ECHO", command).lower()lets the user type quit, Quit, or QUIT.- The loop ends only when the typed word equals
"quit".
-
Create a file called
Task_14.py. -
Part A - Countdown
- Ask the user for a starting number with
input("Start countdown at: ")and convert it toint. - Use a
whileloop to count down to0, printing each number on its own line. - After the loop, print
"Launch!".
- Ask the user for a starting number with
-
Part B - Password gate
- Set
password = "letmein". - Use a
whileloop that keeps askinginput("Password: ")until the user enters the correct password or types"quit". - If the user types the right password, print
"Access granted"and stop the loop withbreak. - If they type
"quit", print"Goodbye"and stop the loop. - For any other input, print
"Incorrect - try again"and continue looping.
- Set
Run the script and test each path: successful login, quitting, and repeated wrong attempts - to see how while, break, and a changing condition control program flow.