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

Tuesday

The menu and adding entries
// A real tool, one piece at a time
⏱ about 30 min

Tuesday: The menu and adding entries

Mission Log can load and save, but nobody can use it yet. Rocket wants the menu working today, even though only one feature is built.

"What happens if someone picks List?" he asks. "That part does not exist yet."

Raven thinks of the construction signs on the road outside. "What do you notice about a road that is still being built?" she asks. "It is open where it is finished, and has a sign where it is not."

Nova displays a small sign that reads Coming soon. "Interesting," she says. "Programmers call that a stub. The whole menu works today, and each stub is replaced when its feature is ready."

Rocket deletes the temporary test lines and starts on main.

Adding an entry

add_entry asks two questions. If the text is empty, it saves nothing. If the tag is empty, it uses note. Then it adds today's date with date.today().isoformat(), saves the whole list and reports the entry number.

Type it below make_entry in your file.

def add_entry(entries):
    """Ask for a new entry, add it to the list and save."""
    text = input("What happened? ").strip()
    if text == "":
        print("Nothing typed, so nothing saved.")
        return
    tag = input("Tag (build, test, idea): ").strip().lower()
    if tag == "":
        tag = "note"
    entries.append(make_entry(text, tag, date.today().isoformat()))
    save_entries(entries)
    print(f"Saved entry {len(entries)}.")

The main function and the menu loop

main loads the log once, then loops: show the menu, ask for a choice, run it. Choices 2 to 4 are stubs for now. Anything else gets a polite message.

Type main below add_entry. Then add the last two lines at the very bottom, so main runs when you run the file.

def main():
    """Load the log, then show the menu until the user quits."""
    entries = load_entries()
    print(f"Mission Log ready. Entries loaded: {len(entries)}")
    while True:
        print()
        print("1 Add   2 List   3 Search   4 Stats   5 Quit")
        choice = input("Choose 1 to 5: ").strip()
        if choice == "1":
            add_entry(entries)
        elif choice == "5":
            print("Log saved. See you next mission!")
            break
        elif choice in ["2", "3", "4"]:
            print("Coming soon.")
        else:
            print("Please type a number from 1 to 5.")
if __name__ == "__main__":
    main()

Delete mission_log.json first so your log starts empty. Here is a run where the user adds one entry, tries an empty one, picks a stub, types 7 and then quits:

Mission Log ready. Entries loaded: 0

1 Add   2 List   3 Search   4 Stats   5 Quit
Choose 1 to 5: 1
What happened? Built the menu
Tag (build, test, idea): Build
Saved entry 1.

1 Add   2 List   3 Search   4 Stats   5 Quit
Choose 1 to 5: 1
What happened? 
Nothing typed, so nothing saved.

1 Add   2 List   3 Search   4 Stats   5 Quit
Choose 1 to 5: 2
Coming soon.

1 Add   2 List   3 Search   4 Stats   5 Quit
Choose 1 to 5: 7
Please type a number from 1 to 5.

1 Add   2 List   3 Search   4 Stats   5 Quit
Choose 1 to 5: 5
Log saved. See you next mission!
The user typed the tag Build. What tag is saved? (Hint: look at add_entry.)
What does the program print when the user picks 4 today?
THE MENU LOOP
  • Read the question.
  • Tap your answer.
Which line ends the menu loop?
Why does add_entry save straight away, not only at Quit?
At your computer
1. Open mission_log.py. Delete the five temporary test lines at the bottom.
2. Type add_entry below make_entry.
3. Type main below add_entry. Then type the two if __name__ lines at the very bottom.
4. Delete mission_log.json from your mission folder, so your log starts empty.
5. Run it. Add one entry, try an empty one, pick 2, type 7, then quit with 5.
6. Run it again and check that it says Entries loaded: 1.

Practice: trace the menu

FOLLOW MAIN AND ADD_ENTRY
  • Read the question.
  • Tap your answer.
The user picks 1 and types only spaces for the text. What happens?
The user types the tag " IDEA ". What tag is saved?
Why does main load the entries only once, before the loop?
The log has 4 entries and the user adds one. What does it print? Saved entry ____.
What tag is saved when the user presses Enter without typing a tag?
Which choice number quits Mission Log?
StatementTrue or false?
A stub lets the menu work before a feature is built.?
return with no value ends add_entry early.?
The menu loop is a while True loop with a break.?
Typing 7 at the menu stops the program with an error.?
WHY THIS EXERCISEUnknown choices reach the else branch, which prints a polite message and shows the menu again.

Mission Log now has a real menu and saves every entry. Tomorrow you will replace the first stub with the List feature.

← Monday