← Back to course
Code Crew G6+ / Week 11 / Wednesday
3/6
Week 11 Β· Build Mission Log

Wednesday

Listing entries
// A real tool, one piece at a time
⏱ about 30 min

Wednesday: Listing entries

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.

Two small functions

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))

Replace the stub

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_entry(7, {"date": "2026-05-01", "tag": "idea", "text": "Add sounds"}) returns a line. What word is inside its square brackets?
The new entry had no tag typed. What tag does it show?
WHY RETURN A STRING?
  • Read the question.
  • Tap your answer.
Why does format_entry return the line instead of printing it?
At your computer
1. Type format_entry below make_entry, and show_entries below add_entry.
2. In main, add the choice 2 branch and remove "2" from the stub list.
3. Save and run it. Pick 2 to see your entries.
4. Add an entry with no tag, then list again.
5. If you see a NameError, check the spelling of each function name.

Practice: test format_entry on its own

# 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))
READ FORMAT_CHECK.PY
  • Read the question.
  • Tap your answer.
What is the first line it prints?
Why can this file test format_entry without the menu?
show_entries is called with an empty list. What does it print?
What number starts the second line it prints?
Which function in Mission Log uses enumerate to number the lines?
What tag is shown for make_entry("Fixed it", "test", "2026-05-03")?
StatementTrue 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.?
WHY THIS EXERCISEformat_entry returns a string for others to use, and show_entries only reads the list to print it.

Mission Log can now show everything it holds. Tomorrow you will add search.

← Tuesday