A menu loop to control user interactions

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

A menu loop to control user interactions

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop keeps a menu showing until the user chooses to quit?
A while True: ... break when the choice is 'Q'
B for i in range(1): ... then the program ends
2. Which order is correct inside a menu loop?
A Show the menu, read the choice, call the matching function
B Call the matching function, read the choice, show the menu
3. Which code dispatches the choice to the right function?
A if choice = '1': add_item() elif choice = '2': view_items()
B if choice == '1': add_item() elif choice == '2': view_items()
4. Why use elif rather than a separate if for each menu option?
A elif makes the menu loop run faster than if
B Only one matching branch runs, and the else can catch anything left over
5. The user types 'x', which is not a menu option. What should the menu loop do?
A Show an error message in an else branch and display the menu again
B End the program without a message

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1while True: AThe quit branch never uses break, so the loop never ends and the menu keeps appearing after the user chooses quit.
2elif choice == "2": BStarts a menu loop that keeps showing the menu until the program reaches a break
3An invalid choice is handled by an else branch that shows an error and lets the loop repeat Celse: print("Invalid choice")
4The quit option ends the menu loop and returns control to the rest of the program DChecks whether the user picked option 2 and, if so, runs that option's function
5while True: choice = input("Choice: ") if choice == "3": print("Bye") EA single = assigns a value instead of comparing. The condition needs == to check the user's choice.
6while True: choice = input("Choice: ") if choice = "1": add_item() Felif choice == "3": break

Definitions

while True:

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
_____ True: choice = input("Choice: ") if choice == "1":
2.
if choice == "1": addItem() ____ choice == "2":
3.
elif choice == "q": break ____:
4.
elif choice == "3": _____ else:
5.
while True: showMenu() choice = _____("Choice: ")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A while True:
B choice = input("Choice: ")
C if choice = "1":
D start_game()
E elif choice == "2":
F break
Question 2
A while True:
B print("1 Play 2 Quit")
C choice = input("Choice: "
D if choice == "2":
E break
F play()
Question 3
A while False:
B choice = input("Choice: ")
C if choice == "Q":
D break
E print("Invalid choice")
Question 4
A while True:
B choice == input("> ")
C if choice == "Q":
D break
E else:
F print("Invalid choice")
Question 5
A while True:
B choice = input("> ")
C if choice == 1:
D view_scores()
E elif choice == "Q":
F break

Answer Key

A menu loop to control user interactions
Section A: Quick Questions
Q1
A — while True: ... break when the choice is 'Q'
Q2
A — Show the menu, read the choice, call the matching function
Q3
B — if choice == '1': add_item() elif choice == '2': view_items()
Q4
B — Only one matching branch runs, and the else can catch anything left over
Q5
A — Show an error message in an else branch and display the menu again
Section B: Match the Pairs
Q1
while True: → Starts a menu loop that keeps showing the menu until the program reaches a break
Q2
elif choice == "2": → Checks whether the user picked option 2 and, if so, runs that option's function
Q3
An invalid choice is handled by an else branch that shows an error and lets the loop repeat → else: print("Invalid choice")
Q4
The quit option ends the menu loop and returns control to the rest of the program → elif choice == "3": break
Q5
while True: choice = input("Choice: ") if choice == "3": print("Bye") → The quit branch never uses break, so the loop never ends and the menu keeps appearing after the user chooses quit.
Q6
while True: choice = input("Choice: ") if choice = "1": add_item() → A single = assigns a value instead of comparing. The condition needs == to check the user's choice.
Section C: Definitions
Q1
while True: — Starts a menu loop that repeats forever: show options, read a choice, call the matching function with if/elif, reject invalid input, and break when the user picks quit.
Section D: Fill in the Blanks
Q1
while True: choice = input("Choice: ") if choice == "1":
Q2
if choice == "1": addItem() elif choice == "2":
Q3
elif choice == "q": break else:
Q4
elif choice == "3": break else:
Q5
while True: showMenu() choice = input("Choice: ")
Section E: Spot the Error
Q1
C — A single = assigns a value and is not allowed in an if condition. Use == to compare the user's choice with "1".
Q2
C — The input() call is missing its closing bracket, which causes a SyntaxError. It should end with ) like this: input("Choice: ").
Q3
A — A menu loop must start with while True: so it keeps running until the user chooses to quit. while False: never runs the menu at all.
Q4
B — == compares values and does not store anything, so choice is never created. Use a single = to assign the user's input to choice.
Q5
C — input() always returns a string, so choice is "1" and never equals the number 1. The menu option would never be matched. Compare with the string "1".