← Console bridge: text in, text out

A menu that keeps showing

Put a menu inside while True, choose with if and elif, and leave the loop with break when the person picks quit.

Ticket machines, ATMs and the settings on a phone are all menus: show the choices, wait for one, do it, and show the menu again.

A while loop stops when its condition turns false. A menu has no such moment: it should keep showing until the person says stop. So it loops for ever and leaves from the inside, with break.

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
total = 0
while True:
    choice = input("1 add $5, 2 pay: ")
    if choice == "1":
        total += 5
        print("Total: $" + str(total))
    elif choice == "2":
        print("Pay $" + str(total))
        break
    else:
        print("Please choose 1 or 2.")
print("Thank you")

while True and break are new. Read the program. What is shown?

You type 1, 1, 3, then 2. What does the program show?

RunRun it and compare the result with your prediction.

Run

Run it. Add a few items, try a wrong choice, then pay.

Line What it does Why
while True: A loop whose condition is never false The menu keeps showing until something inside ends it
choice = input(...) Shows the menu and waits Asked again on every pass
if / elif / else Picks what to do for each choice The else catches anything else, so a wrong choice gets a reply
break Leaves the loop straight away The program carries on with the first line after the loop
lesson.py
total = 0
while True:
    choice = input("1 add $5, 2 pay: ")
    if choice == "1":
        total += 5
        print("Total: $" + str(total))
    elif choice == "2":
        print("Pay $" + str(total))
        break
    else:
        print("Please choose 1 or 2.")
print("Thank you")
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

lesson.py
total = 0
while True:
    choice = input("1 add $5, 2 pay: ")
    if choice == "1":
        total += 5
        print("Total: $" + str(total))
    elif choice == "2":
        print("Pay $" + str(total))
        break
    else:
        print("Please choose 1 or 2.")
print("Thank you")

A session is the list of choices typed.

A menu with no way out.

A one-word answer.

You type 1, 1, 3, 2. Fill in the total after each choice and whether the loop goes round again.

choicetotal afterloop goes round again
15yes
1
3
2

Choosing 2 shows Pay $10, but then the menu comes back and the program never ends. Click the line where something is missing.

What does break do when it runs inside an if inside a while True?

ModifyChange the code on purpose and check the result.

Modify

Add a drink for $2 as choice 3. Paying becomes choice 4. Update the menu text too.

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

Add an elif for a new choice before the pay choice, and change the menu text.

Another hint

Pay becomes choice 4, so change the pay elif too.

Add a drink for $2 as choice 3; paying becomes choice 4.

    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 the last lessons: a loop that repeats, and an answer stored again each pass.

    lesson.py
    total = 0
    while True:
        choice = input("1 add $5, 2 pay: ")
        if choice == "1":
            total += 5
            print("Total: $" + str(total))
        elif choice == "2":
            print("Pay $" + str(total))
            break
        else:
            print("Please choose 1 or 2.")
    print("Thank you")
    

    You type 3, 3, 2. How many times does the menu question appear?

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

    Make

    Make your own menu app: a café, a game shop, a quiz scoreboard or a unit converter. It needs while True, at least three choices, a reply for a wrong choice, and a way to quit.

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

    A menu starts with while True: and an input line inside it.

    Another hint

    Use if / elif for each choice and else for anything else. One choice runs break.

    A menu app: while True, at least three choices with if and elif, and break to quit.

      Run your program. This ticks when your code does all of the above.

      For teachers

      Students build a menu with while True, choose with if and elif, and leave with break. 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 three real beliefs: break only leaves the if, a wrong choice ends the program, and nothing ends it.
      2. Run: students try all the choices, including a wrong one. Ask which line stopped the loop.
      3. Investigate: the bug has no break line at all, so the loop cannot end. Ask what ends a while True.
      4. Make: students choose their theme. Check each program has an else for a wrong choice.

      The hard part (on purpose)

      Forgetting a way out. Ask what makes a while True stop.

      When students get stuck

      Stretch and support

      Stretch: Add a choice that clears the total and starts the order again.

      Support: Give the menu with the choices as comments and ask the student to fill in each branch.

      Where it leads

      Next lesson: how to test a menu without typing the same choices over and over.

      What each step is for

      Now you can
      Next lesson10. Test with a tableWrite test cases (what to type and what the program should reply) as lines that print True or False, and include a wrong choice.