Writing Your Own Functions · capstone
Capstone project: Project: Rebuild the Interactive Menu with Functions
In Chapter 7, you built an interactive menu as one long
Now you will rebuild the same behavior with
This is a refactor: changing the structure of working code while preserving what it does.
Compare the two shapes
The original menu placed every detail inside its branches:
if command == "add":
new_item = input("What should I add? ").strip()
items.append(new_item)
print(f"Added: {new_item}")
elif command == "list":
if len(items) == 0:
print("The list is empty.")
else:
number = 1
for item in items:
print(f"{number}. {item}")
number += 1
elif command == "count":
print(f"Items stored: {len(items)}")
The refactored loop delegates those actions:
if command == "add":
add_item(items)
elif command == "list":
show_items(items)
elif command == "count":
show_count(items)
The total program is not necessarily shorter. It is easier to navigate:
run_menuexplains the overall conversation.add_itemexplains adding.show_itemsexplains listing.show_countexplains counting.
Each function can be read and checked on its own.
The four functions
add_item(items)
Ask exactly:
What should I add?
Clean the answer with .strip(), append it to items, and print:
Added: read a book
The
show_items(items)
If the list is empty, print:
The list is empty.
Otherwise print every item on a numbered line starting at 1.
show_count(items)
Print:
Items stored: 2
using the list’s actual length.
run_menu(items)
This function owns the loop. It asks exactly:
Command (add, list, count, quit):
Clean the command with .strip().lower().
For add, list, and count, call the matching function. For quit, print Goodbye. and break. For anything else, print I do not know that command. and continue.
The top-level program
After the definitions:
choose a non-empty
menu_title;create an empty list named
items;print the title;
call
run_menu(items);print
Final count:followed by the final length.
Those five lines describe the lifetime of the program without explaining every menu action.
Build and test in pieces
Do not write the entire project before running it.
Write
show_count, then call it temporarily with a list of two values.Write
show_items, testing both an empty list and a list with two values.Write
add_item, call it once, and inspect the list afterward.Write
run_menuwith only thequitbranch.Add one command branch at a time. Each branch should delegate to its function.
Remove temporary calls, then run the complete top-level program.
Work through this sequence:
listwhile empty;adda first item;ADDin capital letters and add a second item with spaces around it;count;an unknown command;
list;quit.
The behavior should match your Chapter 7 project. The difference is what the code communicates.
Read the result at two levels
First, read only the top-level lines and run_menu. Can you understand the whole program’s plan?
Then open show_items. Can you understand listing without reading the input and add logic?
That separation is the outcome of this chapter. Functions are not just a way to avoid copying code. They let you design where each piece of knowledge belongs.
If your plan includes Guided, you can ask Monty:
Read my function names and tell me whether each one has one clear responsibility. Point to one boundary I should reconsider, but do not rewrite the project.
You now know how to turn a working sequence of instructions into named, testable operations. Chapter 9 will give those functions richer information to work with by introducing
Task
Rebuild the Chapter 7 interactive menu with four
Requirements:
Define
add_item(items). It asksWhat should I add?, strips the answer, appends it, and printsAdded:followed by the item.Define
show_items(items). It printsThe list is empty.for an empty, or every item as a numbered line starting at 1.Define
show_count(items). It printsItems stored:followed by the length.Define
run_menu(items). It repeatedly asksCommand (add, list, count, quit):and cleans the answer with.strip().lower().Inside
run_menu, theadd,list, andcountbranches must call the matching functions.quitprintsGoodbye.and ends the. An unknown command prints
I do not know that command.and the loop continues.After all function definitions, give
menu_titlea non-emptyof your choice and create an empty list named items.Print the title, call
run_menu(items), then printFinal count:followed by the final length.
Choose your own title and your own items. Keep every specified prompt and message exact so the behavior can be checked.