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

Monday

Plan the tool
// A real tool, one piece at a time
⏱ about 30 min

Monday: Plan the tool

The flight director has a request for the crew: a tool for keeping a log of every mission. It should add notes, list them, search them and count them, and it must never lose a note.

Rocket is already typing. "I have an idea! A menu, a loop, a file, and..." He stops. "Actually, where do I even start?"

Raven pulls the whiteboard over. "What do you notice about the request?" she asks. "It is four features and one rule. That is five small problems, not one big one."

Nova draws five boxes on the whiteboard and an arrow from each to a single file. "Would you like a hint?" she asks. "Decide what one entry looks like first. Every feature uses the same data."

Rocket puts down the keyboard and picks up a marker.

Decompose the problem

FeatureFunctionBuilt on
Load and save the logload_entries, save_entriesMonday
Add an entrymake_entry, add_entry, mainTuesday
List entriesformat_entry, show_entriesWednesday
Searchsearch_entries, run_searchThursday
Count tagscount_tags, show_statsFriday

The shape of one entry

Each entry is a dictionary with three keys: the date, a one-word tag, and the text. The whole log is a list of these dictionaries, saved in mission_log.json. You met this exact shape in week 5.

{"date": "2026-04-12", "tag": "build", "text": "Built the launch clock"}

The menu loop

Load the log
↓
Show the menu
↓
Ask for a choice from 1 to 5
↓ DECISION ↓
1 to 4: run that feature, then show the menu again
↓
5: say goodbye and stop

Part 1: load and save

Start a new file called mission_log.py. The first piece loads and saves the log. The function load_entries uses try and except from week 10, so the first run starts empty.

The test lines at the bottom are temporary. They prove the pieces work before anything else is built.

# mission_log.py
# Mission Log: keep dated notes about your missions.
# Entries are saved in mission_log.json, in the same folder.

import json
from datetime import date

LOG_FILE = "mission_log.json"


def load_entries():
    """Read saved entries, or start an empty list on the first run."""
    try:
        with open(LOG_FILE) as f:
            return json.load(f)
    except FileNotFoundError:
        return []


def save_entries(entries):
    """Write every entry to the JSON file."""
    with open(LOG_FILE, "w") as f:
        json.dump(entries, f, indent=2)


def make_entry(text, tag, day):
    """Build one entry as a dictionary."""
    return {"date": day, "tag": tag, "text": text}


# Temporary test lines: delete them tomorrow.
entries = load_entries()
print("Loaded", len(entries), "entries")
entries.append(make_entry("Planned Mission Log", "idea", "2026-04-12"))
save_entries(entries)
print("Saved", len(entries), "entries")

The first run, then the second run:

Loaded 0 entries
Saved 1 entries
Loaded 1 entries
Saved 2 entries
After a third run, what does the first line say? Loaded ____ entries
Which function makes one entry as a dictionary?
PLANNING
  • Read the question.
  • Tap your answer.
Why decide the shape of an entry before writing features?
At your computer
1. In IDLE, choose File, then New File. Save it in your mission folder as mission_log.py.
2. Type the program above, including the comments and the temporary test lines.
3. Run it. Check for Loaded 0 entries and Saved 1 entries.
4. Run it again. Check for Loaded 1 entries and Saved 2 entries.
5. Open mission_log.json in IDLE and read the two entries.
PRACTICE: PLAN BEFORE YOU BUILD
  • Read the question.
  • Tap your answer.
make_entry("Fixed the clock", "build", "2026-04-13") gives back what?
What does load_entries give back on the very first run?
Why are the test lines at the bottom only temporary?
Which feature should be built first?
How many keys does each entry have?
What is the name of the file the log is saved in?
Which mode does save_entries open the file with?

A plan, a data shape and two working pieces: a strong start. Tomorrow you will build the menu loop and the add feature.