Students build a menu with while True, choose with if and elif, and leave with break.
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.
Predict and Run 9 min · Investigate 9 min · Modify 5 min · Mix it up 3 min · Make 9 min
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.
Swap seats and use your partner's menu app. Try every choice and a wrong one.
Forgetting a way out. Ask what makes a while True stop.
| What you will see | A question to ask | The 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. |
One possible solution for each task. Other correct programs are fine.
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")
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: 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.
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.
Next lesson: how to test a menu without typing the same choices over and over.