← Console bridge: text in, text out

Repeat until

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.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

lesson.py
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.

You type hi, then yo, then quit. What does the program show?

RunRun it and compare the result with your prediction.

Run

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
lesson.py
word = input("Type quit to stop: ")
while word != "quit":
    print("You typed " + word)
    word = input("Type quit to stop: ")
print("Bye")
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

lesson.py
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.

typedword != "quit"shows
hiTrueYou typed hi
yo
quit

The very first word typed is quit. How many times does the indented part of the loop run?

Type hi and the program shows You typed hi again and again and never stops. Click the line that is in the wrong place.

ModifyChange the code on purpose and check the result.

Modify

Change the program so typing either quit or exit stops it.

Try it yourselfYour edits are saved in this browser.
Need a hint?

The loop keeps going while the word is not quit AND the word is not exit.

Another hint

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.

    InvestigateTrace how each line of code connects to what you see.

    Mix it up

    This mixes while with the check from last lesson: ask again until the answer is good.

    lesson.py
    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?

    MakeBuild something new using what you have learned. Expect a few tries; that is normal.

    Make

    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.

    Try it yourselfYour edits are saved in this browser.
    Need a hint?

    Ask for the password once before the loop, then ask again inside it.

    Another hint

    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.

      For teachers

      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.

      Students will be able to

      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).

      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

      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.

      Where it leads

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

      What each step is for

      Now you can
      Next lesson7. Running totalKeep a total across the passes of a loop with +=, setting it to 0 once before the loop starts.