Keep running lines with while for as long as a condition is true, so a program can keep asking until it gets what it needs.
A PIN pad lets you try again, a game keeps going until you win, and a form keeps asking until the answer is valid. All of them repeat until something changes.
So far every program ran from the top to the bottom once. A for loop repeats a set number of times. A while loop repeats for as long as a condition stays true, so a program can keep asking until it gets what it needs.
Make your prediction first. Help opens once you've locked it in.
word = input("Type quit to stop: ")
while word != "quit":
print("You typed " + word)
word = input("Type quit to stop: ")
print("Bye")
while is new, and it works like if that keeps going. Read the code and decide what is shown.
Run it and type a few words, then quit.
| Line | What it does | Why |
|---|---|---|
word = input(...) |
Asks the first time | The loop needs a value to test before it starts |
while word != "quit": |
Asks: is word different from quit? True: run the indented lines, then ask again |
The question is asked before every pass |
word = input(...) |
Asks again, at the end of every pass | This is what can make the condition false |
print("Bye") |
Runs once, after the loop | Not indented, so not part of the loop |
word = input("Type quit to stop: ")
while word != "quit":
print("You typed " + word)
word = input("Type quit to stop: ")
print("Bye")
word = input("Type quit to stop: ")
while word != "quit":
print("You typed " + word)
word = input("Type quit to stop: ")
print("Bye")
Each answer decides whether the loop goes round again.
A loop can run no times at all.
A loop that never ends.
Fill in the condition and what the program shows after each word is typed.
| typed | word != "quit" | shows |
|---|---|---|
| hi | True | You typed hi |
| yo | ||
| quit |
Type hi and the program shows You typed hi again and again and never stops. Click the line that is in the wrong place.
Change the program so typing either quit or exit stops it.
The loop keeps going while the word is not quit AND the word is not exit.
Write while word != "quit" and word != "exit":
Make the loop stop when the person types quit or exit.
Run your program. This ticks when your code does all of the above.
This mixes while with the check from last lesson: ask again until the answer is good.
text = input("Pick a number from 1 to 9: ")
while text.isdigit() == False:
text = input("Digits only. Try again: ")
print("You picked " + text)
The person types two, one, then 7. How many times does Digits only appear?
Write a password door. Choose a secret word. The program asks for the password, says Wrong, try again for every wrong guess, and prints Welcome in! when it is right.
Ask for the password once before the loop, then ask again inside it.
The condition is: the attempt is not the secret.
Keep asking for the password until it is right, then print a welcome.
Run your program. This ticks when your code does all of the above.
Students meet while, a loop that keeps going for as long as a condition is true, and use it to keep asking until the answer is right. About 35 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Seeing that the check happens before every pass. Ask students to say what it asks after the second answer.
Stretch: Allow the door to give a hint after every wrong guess.
Support: Give the loop with the condition missing and ask for the line that sets it.
Next lesson: the loop keeps a running total, adding to a number on every pass.