0%

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 is the shape behind an enormous amount of software, from a tool to the app on your phone: wait for an instruction, carry it out, wait again.

Everything you need is already yours. A to hold the data, a loop to keep the program alive, and 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:

CommandWhat it does
addAsks what to add, then stores it
listShows the stored items as a numbered list
countReports how many items are stored
quitSays 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:

SituationMessage
After addingAdded: read a book
Listing items1. read a book, then 2. water the plants, and so on
Listing when nothing is storedThe list is empty.
CountingItems stored: 2
QuittingGoodbye.
An unknown commandI do not know that command.
After the loopFinal 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:

  1. list on an empty program, which should say the list is empty;

  2. add twice, with a real item each time;

  3. count, which should report 2;

  4. a nonsense command, which should not end the program;

  5. list, which should number both items from 1;

  6. ADD in capital letters, which should still work;

  7. an item typed with spaces around it, which should be stored without them;

  8. 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 , so 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:

  1. Give menu_title a non-empty of your choice and print it once, before the .

  2. Start items as an empty .

  3. Loop until the person quit.

  4. Ask for a command with the prompt Command (add, list, count, quit): and clean it with .strip().lower().

  5. add asks What should I add? , appends the answer cleaned with .strip(), and prints Added: followed by the item.

  6. list prints The list is empty. when nothing is stored, and otherwise prints each item as a numbered line starting at 1.

  7. count prints Items stored: followed by the number stored.

  8. quit prints Goodbye. and leaves the loop.

  9. Any other command prints I do not know that command. and the loop continues.

  10. 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.