Level 0 / Project 11 - Simple Menu Loop¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| Concept | This project | — | Quiz | Flashcards | Diagram | Browser |
Estimated time: 25 minutes
Focus¶
- while loops and command menu flow
Why this project exists¶
Build a text-based menu that dispatches numbered choices to action functions. You will practise while loops, if/elif dispatch, and processing batch commands from a file -- the pattern behind every CLI tool.
Run (copy/paste)¶
Use <repo-root> as the folder containing this repository's README.md.
cd <repo-root>/projects/level-0/11-simple-menu-loop
python project.py --input data/sample_input.txt
pytest -q
Expected terminal output¶
=== Simple Menu ===
1. Greet
2. Reverse text
3. Count characters
4. Quit
[1] => Hello, World!
[2] => nohtyP
[3] => "programming" has 11 characters
3 commands processed. Output: data/output.json
5 passed
Expected artifacts¶
data/output.json- Passing tests
- Updated
notes.md
Checkpoint: Baseline code runs and all tests pass. Commit your work before continuing.
Alter it (required) — Extension¶
- Add a new menu action: "5. Count vowels" that counts vowels in a given string.
- Add a
--verboseflag that prints the menu before each action in batch mode. - Re-run script and tests.
Break it (required) — Core¶
- Enter choice
99-- doesexecute_choice()handle unknown options gracefully? - Enter choice
quitin the middle of the batch file -- doesrun_batch()stop processing? - Enter an action with no argument like just
1with no name -- what happens?
Fix it (required) — Core¶
- Ensure
execute_choice()returns a clear error message for unknown choices. - Handle missing arguments by using a default value (e.g. "World" for greet).
- Add a test for the missing-argument edge case.
Checkpoint: All modifications done, tests still pass. Good time to review your changes.
Explain it (teach-back)¶
- Why does
run_batch()use a for loop instead of a while loop? - What is the advantage of mapping menu choices to functions in the
actionsdict? - Why does the menu loop
breakon "quit" instead of usingsys.exit()? - Where would menu-driven interfaces appear in real software (CLI tools, admin consoles, kiosk systems)?
Mastery check¶
You can move on when you can: - run baseline without docs, - explain one core function line-by-line, - break and recover in one session, - keep tests passing after your change.
Related Concepts¶
| ← Prev | Home | Next → |
|---|---|---|