This guide explains Build a Simple Menu-Driven Console App through a focused example and practical reasoning. No prior knowledge of this specific operation is required.
Understanding the Concept
A menu-driven console application repeats a prompt, chooses an action and stops when the user selects Exit. Separating the menu from the action code avoids confusing nesting.
When You Would Use This
Build a minimal help menu that does not modify files or accounts.
Step-by-Step Example
while True:
print("1. Show greeting | 2. Show tip | 0. Exit")
selected_option = input("Choose: ").strip()
if selected_option == "0":
break
elif selected_option == "1":
print("Hello!")
elif selected_option == "2":
print("Practice a little every day")
else:
print("Unknown option")
How the Example Works
The menu is displayed after each action until 0 is entered. Comparing text avoids needing numeric conversion for the menu selections.
Try It Yourself
Add a third option that displays a short list of your learning goals.
Common Beginner Mistakes
- Not defining a clear exit option.
- Using separate independent
ifblocks that run unintended actions. - Assuming a menu selection is already an integer.
Key Takeaways
- A menu-driven console application repeats a prompt, chooses an action and stops when the user selects Exit.
- The menu is displayed after each action until
0is entered. - Avoid this mistake: Not defining a clear exit option.