← Teacher guide

Repeat until

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.

Students will be able to

Timing

Predict and Run 9 min · Investigate 10 min · Modify 4 min · Mix it up 3 min · Make 9 min

Running the lesson

  1. 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.
  2. Run: run it with a few words then quit. Ask: when is the condition checked?
  3. Investigate: the zero-times question is the idea students miss. The bug is a very common slip: the update dedented.
  4. 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.

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 seeA question to askThe 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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")

Make

secret = "open sesame"
attempt = input("Password: ")
while attempt != secret:
    print("Wrong, try again.")
    attempt = input("Password: ")
print("Welcome in!")

Stretch and support

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.

Exit ticket

A while loop's condition is already False the first time it is checked. How many times does the body run?

Zero times. while checks before every pass, including the first.

What each step is for

Where it leads

Next lesson: the loop keeps a running total, adding to a number on every pass.