A menu loop that controls user interaction

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

A menu loop that controls user interaction

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop header keeps a menu running until the user types q to quit?
A while choice != "q":
B while choice == "q":
2. What is the correct order of steps inside one pass of a menu loop?
A Read a choice, call the function, then show the menu
B Show the menu, read a choice, call that option's function
3. A user types "x", which is not a menu option. What should the menu loop do?
A Print an invalid choice message and show the menu again
B End the program straight away
4. Which line belongs in the loop to handle the quit option?
A elif choice == "q": print("Goodbye")
B elif choice == "q": choice = "a"
5. Why must the choice be read INSIDE the while loop?
A So the menu is only shown once
B So the user can pick a new option on every repeat

Match the Pairs

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

Left columnRight column
1while choice != "q": AKeeps the menu repeating until the user types q to quit
2else: print("Invalid choice") BThere is no else branch, so an invalid choice is ignored silently. Add else: print("Invalid choice") so the user knows to try again.
3Each valid menu option calls its own function inside the loop Cwhile choice != "q": choice = input("Choice: ")
4The menu must read a new choice on every pass so the user can quit DShows an invalid choice message when the input matches no menu option, then the menu appears again
5while choice != "q": show_menu() if choice == "1": view_students() Eif choice == "1": view_students()
6while choice != "q": choice = input("Choice: ") if choice == "1": view_students() elif choice == "2": add_student() FThe choice is never read inside the loop. choice never changes, so the loop never ends and the user cannot quit. Add choice = input(...) after show_menu().

Definitions

while choice != "q":

Fill in the Blanks

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

1.
choice = "" _____ choice != "q": print_menu()
2.
choice = input("Choice: ") while choice __ "q":
3.
____ choice == "2": show_students()
4.
____: print("Invalid choice, try again")
5.
choice = _____("Enter choice: ") run_option(choice)

Spot the Error

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

Question 1
A choice = ""
B while choice == "q":
C choice = input("Option: ")
D if choice == "1":
E view_items()
Question 2
A choice = ""
B while choice != "q":
C show_menu
D choice = input("> ")
Question 3
A choice = ""
B while choice != "q":
C choice = int(input("> "))
D if choice == "1":
E add_item()
Question 4
A def main:
B choice = ""
C while choice != "q":
D choice = input("> ")
Question 5
A choice = ""
B while choice != "q":
C choice = input("> ")
D if choice == 1:
E view_items()
F elif choice == "2":

Answer Key

A menu loop that controls user interaction
Section A: Quick Questions
Q1
A — while choice != "q":
Q2
B — Show the menu, read a choice, call that option's function
Q3
A — Print an invalid choice message and show the menu again
Q4
A — elif choice == "q": print("Goodbye")
Q5
B — So the user can pick a new option on every repeat
Section B: Match the Pairs
Q1
while choice != "q": → Keeps the menu repeating until the user types q to quit
Q2
else: print("Invalid choice") → Shows an invalid choice message when the input matches no menu option, then the menu appears again
Q3
Each valid menu option calls its own function inside the loop → if choice == "1": view_students()
Q4
The menu must read a new choice on every pass so the user can quit → while choice != "q": choice = input("Choice: ")
Q5
while choice != "q": show_menu() if choice == "1": view_students() → The choice is never read inside the loop. choice never changes, so the loop never ends and the user cannot quit. Add choice = input(...) after show_menu().
Q6
while choice != "q": choice = input("Choice: ") if choice == "1": view_students() elif choice == "2": add_student() → There is no else branch, so an invalid choice is ignored silently. Add else: print("Invalid choice") so the user knows to try again.
Section C: Definitions
Q1
while choice != "q": — A menu loop repeats: show the menu, read a choice, call that option's function, and stop only when the user picks quit. Other input gets an invalid choice message.
Section D: Fill in the Blanks
Q1
choice = "" while choice != "q": print_menu()
Q2
choice = input("Choice: ") while choice != "q":
Q3
elif choice == "2": show_students()
Q4
else: print("Invalid choice, try again")
Q5
choice = input("Enter choice: ") run_option(choice)
Section E: Spot the Error
Q1
B — The loop must keep running until the user picks quit, so the condition should be != "q". With == "q" the loop never starts because choice begins as an empty string.
Q2
C — A function is only called when it has brackets. Without them the menu is never shown. It should be show_menu().
Q3
C — int() would crash when the user types q to quit, and a number would never equal the string "q". Menu choices should stay as strings: choice = input("> ").
Q4
A — A function definition needs brackets after its name. It should be def main():.
Q5
D — input() returns text, so choice is the string "1". Comparing it to the number 1 is never true and the option's function would never run. It should be if choice == "1":.