Repeating Work with Loops · capstone
Capstone project: Project: Build an Interactive Menu
The previous lesson kept asking until it received one usable answer, then stopped. This project keeps working after each successful action, until the person using it says they are finished.
That is a real shift. A menu
Everything you need is already yours. A elif to choose an action, break to leave when asked, and a for loop to display the collection.
What the program does
Your program keeps a list of items. The subject is yours to choose: books to read, plants to buy, places to visit, songs to learn, or anything else you like.
It understands four commands:
| Command | What it does |
|---|---|
add | Asks what to add, then stores it |
list | Shows the stored items as a numbered list |
count | Reports how many items are stored |
quit | Says goodbye and ends the loop |
Anything else gets a polite message and another turn.
The exact wording
The commands, prompts, and messages are fixed so that your program can be tested automatically. Everything else, including the title and the items themselves, is yours.
Ask for a command with exactly this prompt:
Command (add, list, count, quit):
Ask for a new item with exactly this prompt:
What should I add?
Print these messages exactly, with the real values in place of the examples:
| Situation | Message |
|---|---|
| After adding | Added: read a book |
| Listing items | 1. read a book, then 2. water the plants, and so on |
| Listing when nothing is stored | The list is empty. |
| Counting | Items stored: 2 |
| Quitting | Goodbye. |
| An unknown command | I do not know that command. |
| After the loop | Final count: 2 |
Clean every answer with .strip(). Clean the command with .strip().lower() as well, so ADD and add both work. Keep the item text as the person typed it, apart from the surrounding spaces. Someone adding The Left Hand of Darkness should not find the left hand of darkness in their list.
Build it in five steps
Run your program after each step. A menu loop is much easier to build in pieces than to write all at once.
1. The loop that ends
Print your title, then write a while True loop that asks for a command and does nothing else except break on quit. Add the Final count: line after the loop.
Run it. Type quit. If the program ends, the skeleton is sound.
2. The unknown command
Add the else branch that prints I do not know that command. Run it, type something silly, and confirm you get another turn rather than a crash.
3. Adding
Add the add branch. It asks the second question, appends the cleaned answer to items, and prints the confirmation. Temporarily print len(items) to check that the list is really growing, then remove that diagnostic before you build the display.
4. Counting and listing
Add the count branch, then the list branch. Inside list, handle the empty list first. Otherwise loop over the items and keep a counter beside the loop, using the accumulator pattern from lesson 6: start it at 1 before the loop, print it, and add 1 at the end of each pass.
5. Personalize
Choose your title and your subject. Add three or four real items and read the output as a user would. Does anything look abrupt or unclear?
Test it before submitting
Work through this sequence by hand and check each response:
liston an empty program, which should say the list is empty;addtwice, with a real item each time;count, which should report2;a nonsense command, which should not end the program;
list, which should number both items from1;ADDin capital letters, which should still work;an item typed with spaces around it, which should be stored without them;
quit, which should print the farewell and the final count.
Submit runs your program with its own prepared answers, so it exercises the same path without you typing anything.
If your plan includes Guided, this is a good moment to ask Monty:
Read my menu loop and tell me whether every branch does what its command promises. Point out one thing I could make clearer, without rewriting it for me.
When it passes
Try adding a fifth command of your own, such as first to show only the first item. You will find that the shape of the program makes room for it easily, which is a sign that the structure is sound.
Then look at the whole program and notice how long the loop body has become. Each branch is short, but they are stacked on top of each other, and understanding any one of them means reading past the others. The next chapter gives you the tool for that: your own named add, list, and count can each become a single line inside the loop and a small piece of code you can read on its own.
You have built a program that stays open, remembers what it has been told, responds to instructions, and shuts down when asked. That is a genuinely different kind of program from the one you wrote in Chapter 1.
Task
Build the interactive menu described in the lesson.
Requirements:
Give
menu_titlea non-emptyof your choice and print it once, before the . Start
itemsas an empty. Loop until the person
quit.Ask for a command with the prompt
Command (add, list, count, quit):and clean it with.strip().lower().addasksWhat should I add?, appends the answer cleaned with.strip(), and printsAdded:followed by the item.listprintsThe list is empty.when nothing is stored, and otherwise prints each item as a numbered line starting at1.countprintsItems stored:followed by the number stored.quitprintsGoodbye.and leaves the loop.Any other command prints
I do not know that command.and the loop continues.After the loop, print
Final count:followed by the number stored.
Choose your own title and your own items. Everything in the table of exact messages must match.