Project: to-do list manager

Mini Projects, lesson 3 of 6

🚀 Mini Projects, lesson 3 of 6

Project: to-do list manager

Store tasks as dicts, drive them with typed commands, then save them to a file.

14 min

4 exercises

1 quiz

0/5 solved

Getting Python ready… examples can run in a moment.

A to-do app is a classic first "real" program. Each task is a dictionary, and all tasks live in a list:

{"id": 1, "title": "Buy milk", "done": False}
  1. Add tasks with unique ids
  2. Complete, format and count tasks
  3. Commands: a loop that reads add, done, list and quit
  4. Persistence: save the tasks to tasks.json and load them back at startup
Example

A list of dicts

Comprehensions and generator expressions make filtering and counting one-liners. Add a fourth task.

Exercise

Step 1: add tasks

Write add_task(tasks, title):

  • strip spaces from the title; raise ValueError if it's empty
  • the new id is the biggest existing id + 1 (or 1 for an empty list)
  • append {"id": ..., "title": ..., "done": False} and return that dict
Exercise

Step 2: complete, format, count

Add three functions:

  • complete_task(tasks, task_id) marks that task done and returns True, or returns False if there's no such id
  • format_task(task) returns "[x] 1. Buy milk" for a done task and "[ ] 2. Call mum" otherwise
  • pending_count(tasks) returns how many aren't done

Step 3: commands. The program reads commands like add Buy milk or done 2. Split each one into the action and the rest with command.partition(" "), then hand it to handle(tasks, command), which returns the text to show. Keeping handle free of input() makes it easy to test.

Quiz

What does this print?

Example

Parsing commands

rest is '' when there's nothing after the action. isdigit() lets you check for a number before calling int().

Walkthrough

What the pre-filled commands do to tasks

idtitledone1Buy milkFalse
Step 1 of 4
command = add Buy milk
returns = Added #1: Buy milk

partition gives action add, rest Buy milk. No tasks yet, so max(ids, default=0) + 1 makes id 1.

changed by this command
still to do
Exercise

Step 3: the command loop

Finish handle(tasks, command) so it returns:

  • add <title> → "Added #<id>: <title>", or "Title can't be empty" on a ValueError
  • done <id> → "Completed #<id>", or "No task #<id>" if it doesn't exist
  • list → every format_task line, then "<n> left to do", joined with newlines ("Nothing to do!" when there are no tasks)
  • anything else → "Unknown command: <action>"

The loop at the bottom is done: it reads commands until quit.

Your program receives this input: add Buy milk ⏎ add Call mum ⏎ done 1 ⏎ list ⏎ quit

Step 4: make it remember. So far the list dies with the program. With the files and JSON tools from the Standard Library module, persistence is two small functions: save_tasks(tasks) writes the list to tasks.json, and load_tasks() reads it back — returning [] when the file doesn't exist yet. The loop below starts from load_tasks() and saves after every change, so the tasks survive the program.

Exercise

Step 4: save and load

Write the two persistence functions:

  1. save_tasks(tasks) writes the whole task list to tasks.json with json.dump.
  2. load_tasks() reads tasks.json back and returns the list — or [] if the file doesn't exist yet (catch FileNotFoundError).

Everything else is done: the loop starts from load_tasks() and saves after every change. The check writes a different tasks.json behind your back, so both functions must genuinely use the file. Run the program twice — your tasks survive.

Your program receives this input: list ⏎ add Water plants ⏎ quit