← Teacher guide

A menu that keeps showing

Students build a menu with while True, choose with if and elif, and leave with break.

Console bridge: text in, text out · Menus and testing · 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 9 min · Modify 5 min · Mix it up 3 min · Make 9 min

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.

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)

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

When students get stuck

What you will seeA question to askThe fix
The program never ends.Which line makes the loop stop?One branch needs a break line, indented inside the loop.
A wrong choice makes the program do something odd.What does the program do when none of the choices match?Add an else with a message, such as Please choose 1, 2 or 3.

Answers to the checks

  1. Predict: You type 1, 1, 3, then 2. What does the program show?
    Total: $5, Total: $10, Please choose 1 or 2., Pay $10, Thank you. break leaves the whole loop at once, not just the if, so the program carries on after the loop and ends.
  2. Follow a session: You type 1, 1, 3, 2. Fill in the total after each choice and whether the loop goes round again.
    10, yes, 10, yes, 10, no
    A wrong choice (3) changes nothing, and the menu shows again. Only 2 runs break.
  3. Spot the bug: Choosing 2 shows Pay $10, but then the menu comes back and the program never ends. Click the line where something is missing.
    line 7
    Nothing ever runs break, so while True never ends. The pay branch needs a break line after the print.
  4. What does break do?: What does break do when it runs inside an if inside a while True?
    Leaves the whole loop at once
    break ends the loop it is in. The program continues with the first line after the loop.
  5. Mix it up: You type 3, 3, 2. How many times does the menu question appear?
    3
    Each pass asks once. 3 and 3 are wrong choices that go round again, and 2 asks a third time before it breaks.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify

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

Make

total = 0
while True:
    choice = input("1 coffee $4, 2 cake $5, 3 pay: ")
    if choice == "1":
        total += 4
        print("Coffee added. Total: $" + str(total))
    elif choice == "2":
        total += 5
        print("Cake added. Total: $" + str(total))
    elif choice == "3":
        break
    else:
        print("Please choose 1, 2 or 3.")
print("Please pay $" + str(total))

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.

Exit ticket

What does break do, and what happens to the program after it?

break leaves the loop at once. The program carries on with the first line after the loop.

What each step is for

Where it leads

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