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

Thursday

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

Thursday: Searching the log

The flight director stops by and asks Rocket which entries mention the clock. Rocket picks 2 and starts scrolling with his finger on the screen, counting out loud.

Halfway down, he laughs at himself. "I built a tool so I would not have to do this," he says.

Raven nods toward the stub list in main. "What do you notice?" she asks. "Search is next on the plan. And we already wrote one, for the text log in week 8."

Nova shows the old search and the new data side by side. "Would you like a hint?" she asks. "Return the matching entries as a list, without printing. Then printing them is a separate job."

Rocket writes search_entries so it gives back a list.

Find, then show

search_entries checks each entry. It matches when the word appears in the text, ignoring capitals, or in the tag. It returns a new list and prints nothing, which makes it easy to test next week.

Type it below format_entry.

def search_entries(entries, word):
    """Entries whose text or tag contains word, ignoring capitals."""
    word = word.lower()
    found = []
    for entry in entries:
        if word in entry["text"].lower() or word in entry["tag"]:
            found.append(entry)
    return found

run_search asks for the word, calls search_entries, and prints each match with format_entry. Type it below show_entries.

def run_search(entries):
    """Ask for a word and print the matching entries."""
    word = input("Search for: ").strip()
    found = search_entries(entries, word)
    for number, entry in enumerate(found, start=1):
        print(format_entry(number, entry))
    print(f"Matches found: {len(found)}")

In main, add the choice 3 branch below choice 2, and change the stub list to just ["4"]:

        elif choice == "3":
            run_search(entries)

With the same three saved entries as yesterday, three searches look like this:

Mission Log ready. Entries loaded: 3

1 Add   2 List   3 Search   4 Stats   5 Quit
Choose 1 to 5: 3
Search for: CLOCK
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
Matches found: 3

1 Add   2 List   3 Search   4 Stats   5 Quit
Choose 1 to 5: 3
Search for: idea
1. 2026-04-11 [idea] Add a sound when the clock ends
Matches found: 1

1 Add   2 List   3 Search   4 Stats   5 Quit
Choose 1 to 5: 3
Search for: rocket
Matches found: 0

1 Add   2 List   3 Search   4 Stats   5 Quit
Choose 1 to 5: 5
Log saved. See you next mission!
Search wordMatches "Built the launch clock" (tag build)?
CLOCK?
launch?
bui?
rocket?
test?
WHY THIS EXERCISEin finds the letters anywhere, so part of a word matches too, and capitals do not matter.
Why did the search for CLOCK find 3 entries, not just the one with clock in the tag?
WHY THIS EXERCISEsearch_entries looks in the text as well as the tag, and all three texts contain clock.
At your computer
1. Type search_entries below format_entry, and run_search below show_entries.
2. In main, add the choice 3 branch and change the stub list to ["4"].
3. Save and run it. Search for a word you know is in your log.
4. Search with capital letters. Then search for a word that is not there.
5. Search for just one letter, like e. What do you notice?

Practice: test search_entries

search_entries returns a list, so a short file can check what it finds for different words.

# search_check.py: tries search_entries by itself
def search_entries(entries, word):
    """Entries whose text or tag contains word, ignoring capitals."""
    word = word.lower()
    found = []
    for entry in entries:
        if word in entry["text"].lower() or word in entry["tag"]:
            found.append(entry)
    return found


log = [
    {"date": "2026-04-10", "tag": "build", "text": "Built the launch clock"},
    {"date": "2026-04-11", "tag": "idea", "text": "Add a sound"},
]
print(len(search_entries(log, "Clock")))
print(len(search_entries(log, "idea")))
print(len(search_entries(log, "a")))
print(search_entries(log, "zzz"))
READ SEARCH_CHECK.PY
  • Read the question.
  • Tap your answer.
Why does the search for Clock find 1 entry?
Why does the search for a find both entries?
What does search_entries give back when nothing matches?
How many entries does a search for build find?
How many entries does a search for sound find?
What does len([]) give?
StatementTrue or false?
search_entries prints the matches itself.?
run_search uses format_entry to print each match.?
A search for a single letter can match many entries.?
search_entries changes the original list.?
WHY THIS EXERCISEsearch_entries returns a new list and leaves the log alone, and run_search does the printing.

Search is done, and it reuses format_entry. Tomorrow you will build the last feature and run the complete tool.

← Wednesday