🚀 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}
- Add tasks with unique ids
- Complete, format and count tasks
- Commands: a loop that reads
add,done,listandquit - Persistence: save the tasks to
tasks.jsonand load them back at startup
A list of dicts
Comprehensions and generator expressions make filtering and counting one-liners. Add a fourth task.
Step 1: add tasks
Write add_task(tasks, title):
- strip spaces from the title; raise
ValueErrorif 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
Step 2: complete, format, count
Add three functions:
complete_task(tasks, task_id)marks that task done and returnsTrue, or returnsFalseif there's no such idformat_task(task)returns"[x] 1. Buy milk"for a done task and"[ ] 2. Call mum"otherwisepending_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.
What does this print?
Parsing commands
rest is '' when there's nothing after the action.
isdigit() lets you check for a number before calling
int().
What the pre-filled commands do to tasks
partition gives action add, rest Buy milk. No tasks yet, so max(ids, default=0) + 1 makes id 1.
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 ValueErrordone <id>→"Completed #<id>", or"No task #<id>"if it doesn't existlist→ everyformat_taskline, 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.
Step 4: save and load
Write the two persistence functions:
save_tasks(tasks)writes the whole task list totasks.jsonwithjson.dump.load_tasks()readstasks.jsonback and returns the list — or[]if the file doesn't exist yet (catchFileNotFoundError).
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