The log has grown to dozens of lines. Now the flight director has a question: when did we last test the docking game? Rocket scrolls and scrolls.
"There has to be a search," he says. "I am a programmer. I should not be scrolling."
Raven opens her notebook to a page with a list of line numbers. "What do you notice?" she asks. "We need two things: every line with a number, and only the lines that match a word."
Nova shines a narrow beam across the log, lighting up one line at a time. "Would you like a hint?" she asks. "The in operator works on strings too. It tells you whether one string appears inside another."
Rocket reads the whole log into a list first, so he can use it twice.
"test" in "Engine test passed" is True, because the letters test appear inside the longer string. Comparing lowercase versions makes the search ignore capital letters.
# log_viewer.py
LOG_FILE = "mission_log.txt"
lines = []
with open(LOG_FILE) as f:
for line in f:
lines.append(line.strip())
print(f"{len(lines)} lines in the log")
for number, line in enumerate(lines, start=1):
print(f"{number}. {line}")
word = input("Search for: ").strip().lower()
found = 0
for line in lines:
if word in line.lower():
print(" found:", line)
found = found + 1
print(found, "matching lines") With this four-line log in the folder, and a search for TEST:
4 lines in the log 1. 2026-04-10 Started the engine test 2. 2026-04-10 Engine test passed 3. 2026-04-11 Fixed the countdown clock 4. 2026-04-11 Tested the docking game Search for: TEST found: 2026-04-10 Started the engine test found: 2026-04-10 Engine test passed found: 2026-04-11 Tested the docking game 3 matching lines
A program can read one file and write another. This one copies every line about tests into a new file called tests.txt.
# export_tests.py
kept = 0
with open("mission_log.txt") as source:
with open("tests.txt", "w") as target:
for line in source:
if "test" in line.lower():
target.write(line)
kept = kept + 1
print("Saved", kept, "lines to tests.txt") Saved 3 lines to tests.txt
| Statement | True or false? |
|---|---|
| A program can have two files open at once. | ? |
| "test" in "Tested" is True. | ? |
| "test" in "tested" is True. | ? |
| Mode "w" on tests.txt keeps last run's results. | ? |
You can now save, grow, number and search a log. That is most of Mission Log already. Tomorrow is a Crew Quest.