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.
Make your prediction first. Help opens once you've locked it in.
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?
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 |
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")
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.
| choice | total after | loop goes round again |
|---|---|---|
| 1 | 5 | yes |
| 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.
Add a drink for $2 as choice 3. Paying becomes choice 4. Update the menu text too.
Add an elif for a new choice before the pay choice, and change the menu text.
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.
This mixes the last lessons: a loop that repeats, and an answer stored again each pass.
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?
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.
A menu starts with while True: and an input line inside it.
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.
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.
Forgetting a way out. Ask what makes a while True stop.
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.
Next lesson: how to test a menu without typing the same choices over and over.