EXERCISE 6: Create a command line menu with while loops and if statements
- Control flow syntax and indentation (revisited)
if(elif, andelse) statements (revisited)whileloops- Menu system pattern
Task: Create a command-line menu using a while loop and if (elif, else) statements.
Start by designing a simple menu for your app. You can build on this as you go along, but the first iteration should have at least the following options:
- Set number of cards the user would like to practice (you might be able to reuse/move some of the code you have already written)
- Start flashcards (just print “Starting flashcards…” for now since you haven’t implemented the app fully yet)
- Display current score (you might be able to reuse/move some of the code you have already written)
- Exit
Example menu
Select an option by entering a number
1: Set the number of cards you wish to practice
2: Start flashcards
3: Show the current score
4: Exit
Then write code that allows the user to select from the four options listed above
based on their input. You can build on this pattern for command line menus which uses a while loop and if statements to continuously ask the user for input until they enter something that triggers the program to move to a different activity (e.g. calculate the score, start the game.)
Bonus Task: Think about what behaviour you would like if the user enters an invalid input, e.g. a number that is not listed or a non-number.
Run and check: Run your code in the terminal to make sure it works with the command
python flashcards_app.py
Test the menu with different inputs to check that each option results in the behaviour you expect (even if it’s just printing out a placeholder message). Other than using the “Exit” option you can also exit a terminal program with [Ctrl + C] or [Cmd + C] (some Mac configurations).
Read through and add comments: Add any comments in your code that will help you understand it when you come back to it later.
Save your progress: Commit with a descriptive message, e.g. “EXERCISE 6: Create a command line menu with while loops and if statements” and save your work to Github with the standard Git workflow.
(Re)read the guides:
- Control flow syntax and indentation (revisited)
if(elif, andelse) statements (revisited)whileloops- Menu system pattern
Example solution
flashcards_app.py
# Created by: Alex Ubuntu
# Date: 01.01.2026
# Purpose: A personal flashcard trainer to help with learning
### INITIAL INTERACTION WITH USER
# Welcome message
print("Welcome to your personal flashcard trainer!")
# Absolute maximum number of cards so that the user can't ask for too many
ABSOLUTE_MAX_CARDS = 100
DEFAULT_MAX_CARDS = 20
# Fetch from user and save to variable
name = input("What is your name? ")
max_cards = DEFAULT_MAX_CARDS # Set a default value for max_cards
# Confirm name
print(f"\nMy name is {name}")
# Card and score variables
num_cards_completed = 0
num_cards_correct = 0
score = 0
### APP MENU LOOP
while True:
# Start on new line for readability using '\n'
print("\nSelect an option by entering a number")
print("1: Set the number of cards you wish to practice")
print("2: Start flashcards")
print("3: Show the current score")
print("4: Exit")
choice = input("\nChoose an option: ")
if choice == "1":
# Fetch from user and save to variable
max_cards = int(input("\nHow many cards would you like to practice each session? "))
# Confirm number maximum number of cards per session
print(f"\nI want to practice at most {max_cards} cards per session")
elif choice == "2":
print("\nStarting flashcards...")
elif choice == "3":
score = (num_cards_correct/num_cards_completed) * 100
# Display score information
print(f"\nYou have answered {num_cards_correct} out of {num_cards_completed} correctly. Your score so far is {score}%.")
# Display feedback message based on score
if score > 90 and score <= 100:
print("Excellent work!")
elif score > 70 and score <= 90:
print("Good job!")
elif score > 50 and score <= 70:
print("Keep practicing!")
elif score > 0 and score <= 50:
print("Need more study time!")
else:
print("Score lies outside range")
elif choice == "4":
print("\nExiting...")
break
else:
# Clarify instruction to get valid input (Bonus Task)
print("\nInvalid value entered. Please make sure you enter just a single digit: 1, 2, 3 or 4, to select an option.")
Take a break: 🎸
