Rocket has added six entries to Mission Log, and he wants to see them all. He picks 2 from the menu and gets Coming soon, which is starting to feel like a joke he is telling himself.
"Time to replace a stub," he says, cracking his knuckles.
Raven looks at last week's log viewer. "What do you notice?" she asks. "We numbered lines before with enumerate. And searching later will need the same line format."
Nova splits the job into two glowing pieces. "I found a pattern," she says. "One function turns an entry into a line of text. Another loops and prints. Then search can reuse the first one."
Rocket writes the formatting function first.
format_entry turns one entry into one line of text and returns it. It does not print, so other features can use it too. Type it below make_entry.
def format_entry(number, entry):
"""One entry as a line of text."""
return f"{number}. {entry['date']} [{entry['tag']}] {entry['text']}" show_entries prints every entry with a number, or a friendly message for an empty log. Type it below add_entry.
def show_entries(entries):
"""Print every entry with its number."""
if len(entries) == 0:
print("No entries yet.")
return
for number, entry in enumerate(entries, start=1):
print(format_entry(number, entry)) In main, add a branch for choice 2 above the choice 5 branch, and take "2" out of the stub list. The middle of main now reads:
if choice == "1":
add_entry(entries)
elif choice == "2":
show_entries(entries)
elif choice == "5":
print("Log saved. See you next mission!")
break
elif choice in ["3", "4"]:
print("Coming soon.") Start from a log with three saved entries. Here the user lists, adds one entry with no tag, and lists again. New entries show the date you run it:
Mission Log ready. Entries loaded: 3 1 Add 2 List 3 Search 4 Stats 5 Quit Choose 1 to 5: 2 1. 2026-04-10 [build] Built the launch clock 2. 2026-04-10 [test] Tested the clock with 65 seconds 3. 2026-04-11 [idea] Add a sound when the clock ends 1 Add 2 List 3 Search 4 Stats 5 Quit Choose 1 to 5: 1 What happened? Planned the list feature Tag (build, test, idea): Saved entry 4. 1 Add 2 List 3 Search 4 Stats 5 Quit Choose 1 to 5: 2 1. 2026-04-10 [build] Built the launch clock 2. 2026-04-10 [test] Tested the clock with 65 seconds 3. 2026-04-11 [idea] Add a sound when the clock ends 4. 2026-04-12 [note] Planned the list feature 1 Add 2 List 3 Search 4 Stats 5 Quit Choose 1 to 5: 5 Log saved. See you next mission!
# format_check.py: tries format_entry by itself
def make_entry(text, tag, day):
"""Build one entry as a dictionary."""
return {"date": day, "tag": tag, "text": text}
def format_entry(number, entry):
"""One entry as a line of text."""
return f"{number}. {entry['date']} [{entry['tag']}] {entry['text']}"
e = make_entry("Painted the rocket", "build", "2026-05-02")
print(format_entry(1, e))
print(format_entry(12, e)) | Statement | True or false? |
|---|---|
| format_entry prints the line itself. | ? |
| A function that returns a string can be reused by other features. | ? |
| enumerate(entries, start=1) numbers the first entry 1. | ? |
| show_entries changes the entries in the list. | ? |
Mission Log can now show everything it holds. Tomorrow you will add search.