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.
Console bridge: text in, text out · Keep asking · about 35 minutes · Open the student lesson
Classroom resources
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
SlidesDownload Present from the browser, or export to PowerPoint or PDF.
WorksheetDownload Print the student copy; the Teacher edition has the answers.
Students will be able to
I can use while to repeat lines until a condition is false.
I can say what makes a loop stop.
I can write a loop that keeps asking until the answer is right.
Timing
Predict and Run 9 min · Investigate 10 min · Modify 4 min · Mix it up 3 min · Make 9 min
Running the lesson
Predict: take a vote. The wrong options are two real beliefs: that quit is shown too, and that the loop runs once. Ask what the program checks before showing anything.
Run: run it with a few words then quit. Ask: when is the condition checked?
Investigate: the zero-times question is the idea students miss. The bug is a very common slip: the update dedented.
Make: students choose their own secret. Early finishers add a count of the tries (next lesson).
In class
On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.
Be the loop(investigate, 4 min)
One student reads the lines, another is the computer. After every answer the computer asks aloud: is the word different from quit?
Hearing the condition asked every time makes the check before each pass concrete.
The hard part (on purpose)
Seeing that the check happens before every pass. Ask students to say what it asks after the second answer.
When students get stuck
What you will see
A question to ask
The fix
The loop never ends.
Which line inside the loop can change the thing the condition tests?
The input line must be inside the loop (indented), so the test sees a new answer each pass.
The loop body never runs.
Is the condition False at the start?
Check the first answer against the condition. A loop only starts when the condition is True.
Answers to the checks
Predict: You type hi, then yo, then quit. What does the program show? You typed hi, then You typed yo, then Bye. The condition is checked before every pass, so once quit is typed the loop body is skipped and quit is never shown.
Follow the loop: Fill in the condition and what the program shows after each word is typed. True, You typed yo, False, Bye The condition is tested after every answer. While it is True the body runs again. When quit is typed it is False, so the body is skipped and the program carries on to Bye.
Zero times: The very first word typed is quit. How many times does the indented part of the loop run? 0 times while checks first. If the condition is False straight away, the body never runs.
Spot the bug: Type hi and the program shows You typed hi again and again and never stops. Click the line that is in the wrong place. line 4 The line that asks again is not indented, so it is outside the loop. Nothing inside the loop changes word, so the condition stays True for ever. Indent it so it runs on every pass.
Mix it up: The person types two, one, then 7. How many times does Digits only appear? 2 two and one both fail the check, so the loop body runs twice. 7 passes, so the loop ends.
Solutions
One possible solution for each task. Other correct programs are fine.
Modify
word = input("Type quit or exit to stop: ")
while word != "quit"and word != "exit":
print("You typed " + word)
word = input("Type quit or exit to stop: ")
print("Bye")