Step 10.3 – Python from Zero – Conditional Menus
Create a command-based menu with options: Add, View, and Exit
🧠 Why this lesson is important
In the first two lessons, we learned how to:
- Lesson 1 → Read input, use variables, and make decisions with
if/else. - Lesson 2 → Store multiple elements in a list and use loops to iterate over them.
Now, we’ll combine these skills to create an interactive program where the user selects actions from a menu and the program responds accordingly.
🎯 Goal
We will build a command-line To-Do List with these features:
- [A] Add a task
- [V] View all tasks
- [X] Exit the program
🏗️ Menu structure
- Display the available options.
- Read the user’s choice.
- Execute the appropriate action using
if/elif/elseormatch/case. - Repeat until the user chooses to exit.
📝 Base code
# todo_menu.py
tasks = [] # Task list
def show_menu():
print("\n=== TO-DO MENU ===")
print("[A] Add task")
print("[V] View tasks")
print("[X] Exit")
def add_task():
text = input("Write the new task: ").strip()
if text:
tasks.append(text)
print(f"✅ '{text}' added to the list!")
else:
print("⚠️ You didn't write anything, task not added.")
def view_tasks():
if not tasks:
print("📭 No tasks available.")
return
print("\n📋 Your tasks:")
for i, t in enumerate(tasks, start=1):
print(f"{i}. {t}")
def main():
print("Welcome to your To-Do List!")
while True:
show_menu()
choice = input("Choose an option: ").strip().lower()
if choice == "a":
add_task()
elif choice == "v":
view_tasks()
elif choice == "x":
print("Goodbye! 👋 See you next time.")
break
else:
print("❌ Invalid command. Try again.")
if __name__ == "__main__":
main()
🔍 How it works
tasks = []→ creates an empty list to store tasks.show_menu()→ displays the available options each time.add_task()→ asks for a text input and adds it to the list if it’s not empty.view_tasks()→ lists tasks or shows a message if there are none.main()→ runs the loop, reads the choice, and calls the corresponding function.
💡 Alternative with match/case (Python ≥ 3.10)
while True:
show_menu()
choice = input("Choose an option: ").strip().lower()
match choice:
case "a":
add_task()
case "v":
view_tasks()
case "x":
print("Goodbye! 👋 See you next time.")
break
case _:
print("❌ Invalid command. Try again.")
🎨 Improving user experience
- Use emojis or symbols to make messages clearer.
- Add blank lines
\nto make the output easier to read. - Always normalize input with
strip()andlower()to avoid errors.
🧩 Exercises
- Task count: after displaying tasks, also show the total number (
len(tasks)). - Multiple task addition: allow adding multiple tasks until the user types
done. - Clear all tasks: add option
[C]to clear the list after confirmation. - Numeric menu: allow numbers
1/2/0as alternative menu options.
🚫 Common mistakes
- Not checking if the list is empty before printing it.
- Comparing input without
strip()orlower()and failing to recognize commands. - Forgetting
breakin the exit condition, causing the program to run indefinitely.
📌 Key takeaways
In this lesson, you learned to:
- Create an interactive menu in Python.
- Handle user input with multiple conditions.
- Organize your code into functions for clarity and maintainability.
In the next lesson, we will add functions to delete and mark tasks as complete, and to save data to a file.
