The flight director wants every important event at Mission Control written down with its date. Not on sticky notes, not on the whiteboard, but in one log file that keeps growing.
"I could write the date by hand each time," Rocket says, "but I would get it wrong at least once a week."
Raven thinks back over the last two weeks. "What do you notice?" she asks. "We know how to get today's date. We know how to append to a file. We know how to wrap a job in a function."
Nova arranges three glowing pieces into one shape. "I found a pattern," she says. "Three things you already know, combined into one new tool."
Rocket writes a function called log_event.
LOG_FILE is written in capitals because it never changes while the program runs. Programmers use capital names for settings like this.
The function log_event() adds one line: today's date, a space and the text.
The function count_lines() reads the file with readlines(), which gives a list of lines, and counts them.
# log_event.py
from datetime import date
LOG_FILE = "mission_log.txt"
def log_event(text):
"""Add one dated line to the log file."""
stamp = date.today().isoformat()
with open(LOG_FILE, "a") as f:
f.write(f"{stamp} {text}\n")
def count_lines(filename):
"""How many lines a text file has."""
with open(filename) as f:
return len(f.readlines())
log_event("Started the engine test")
log_event("Engine test passed")
print("Log has", count_lines(LOG_FILE), "lines") After two runs, mission_log.txt holds four lines. Your dates will be the days you run it:
2026-04-12 Started the engine test 2026-04-12 Engine test passed 2026-04-12 Started the engine test 2026-04-12 Engine test passed
Each log line is a date, a space, then the text. The split(" ", 1) method cuts at the first space only, giving a list of two pieces.
That lets a program count entries per day with a dictionary.
# per_day.py
counts = {}
with open("mission_log.txt") as f:
for line in f:
day, text = line.strip().split(" ", 1)
counts[day] = counts.get(day, 0) + 1
for day, number in counts.items():
print(f"{day}: {number} entries") 2026-04-10: 2 entries 2026-04-11: 2 entries
| Statement | True or false? |
|---|---|
| split(",") cuts a string at every comma. | ? |
| split always gives back exactly two pieces. | ? |
| readlines() gives a list of the lines in a file. | ? |
| A capital name like LOG_FILE tells readers it is a setting. | ? |
You built a dated, growing log. Tomorrow you will build a viewer that numbers and searches it.