Module 06 · Lesson 03
Project: To-Do Task Manager
Build a to-do list manager from lists, dictionaries and functions: add tasks, mark them done, remove them, show them numbered and count what is left.
- About 8 minutes
- Builds on earlier lessons
- 12-question quiz at the end
What you will learn
- Tasks as dictionaries
- One function per action
- Number tasks from 1
- Count what is left
The manager in four parts
A to-do list is a list of tasks. Each task is a small dictionary, and each action is a function.
The data
tasks = [{"title": "Read", "done": False}]: one dictionary per task.One function per job
add_task(),mark_done(),remove_task()andshow_tasks().Shown from 1
People see tasks numbered from 1, so task number 2 is index 1.
Count what is left
A loop counts the tasks whose
doneis stillFalse.
Lists of tasks everywhere
The same data, a list of small records, sits behind many apps.
- To-do appsEach task has a title, a tick and sometimes a due date.
- Shopping listsTick items off as they go in the trolley.
- Quest logsGames track quests as done or not done, with a count of what is left.
- Homework plannersSubjects, tasks and ticks, shown as a numbered list.
Ticking a task off
This line marks one task as done. Tap each part to see its job.
Part 1 of 5
the list:The list of every task.
Build the manager in five steps
Build the project one step at a time. Each example runs on its own: change it, then press Run.
Example 1 of 61. The data
A list of dictionaries: every task has the same two keys.
Your turn: Add a third task, then print the title of the last one with [-1].
tasks = [
{"title": "Feed the cat", "done": False},
{"title": "Homework", "done": True},
]
print(tasks[0]["title"])
print(len(tasks), "tasks")Press Run to see what it prints.Watch tasks get added and ticked
Press Step to run one line at a time. Watch the list in the names box change.
Ready. Press Step to run the first line.
- tasks = []
- tasks.append({"title": "Homework", "done": False})
- tasks.append({"title": "Walk dog", "done": False})
- tasks[0]["done"] = True
- left = 0
- for task in tasks:
- if not task["done"]:
- left += 1
- print(left, "task left")
Output so far
Nothing printed yet.
What Python does
Python reads the program from the top. Nothing has run yet.
Names
None yet.
Quick guesses
Guess first, then read why. A wrong guess is where the learning happens. Nothing here is scored.
Question 1 of 6
1.Shown number 2: what prints?
tasks = ["Read", "Run", "Swim"]
number = 2
print(tasks[number - 1])0 of 6 answered
Common task manager misconceptions
Each card is something beginners often believe. Tap it to see what Python really does.
Misconception 1 of 4
Notes
Everything above, in words. Open it when you want the full explanation.
Read the notesNotesOpenClose
Syntax
The general shape of the statements in this lesson.
task = {"title": "Read", "done": False}
tasks.append(task) # add
tasks[index]["done"] = True # complete
removed = tasks.pop(index) # remove and keep it
for n, task in enumerate(tasks, start=1): # people count from 1
index = int(text) - 1 # back to a list index
if 0 <= index < len(tasks): ... # check before using itPress Run to see what it prints.Worked examples
Read the problem, run the code, then read why it works.
Example 1 of 2
A session without typing
Run a whole session of the manager by calling the functions directly: add three tasks, complete one, try a bad number, remove one and show the result.
def add_task(tasks, title):
tasks.append({"title": title, "done": False})
def to_index(tasks, text):
text = text.strip()
if not text.isdigit():
return None
index = int(text) - 1
return index if 0 <= index < len(tasks) else None
def complete(tasks, text):
index = to_index(tasks, text)
if index is None:
print("No task with that number")
else:
tasks[index]["done"] = True
def show(tasks):
for number, task in enumerate(tasks, start=1):
mark = "x" if task["done"] else " "
print(f"{number}. [{mark}] {task['title']}")
tasks = []
for title in ["Buy milk", "Call Sam", "Water plants"]:
add_task(tasks, title)
complete(tasks, "2")
complete(tasks, "7")
tasks.pop(0)
show(tasks)Press Run to see what it prints.Predict first
How it works(open after your guess)
Common pitfalls
Mistakes people make with this topic, and what to write instead.
Pitfall 1 of 3
Using the shown number as the index
Spot the bug
Can you see what is wrong? Run it to watch it go wrong, then open the fix.
tasks = [{"title": "A", "done": False}, {"title": "B", "done": False}]
number = 2
tasks[number]["done"] = TruePress Run to see what it prints.Show what goes wrong and the fixWhat goes wrong and the fix
In the real world
Where this shows up in software people use every day.
- Home
A chores rota for a shared flat
Flatmates keep their weekly chores in one list. Each chore records who does it and whether it is done, and the rota shows who still has work left.
Why Python fits
The same task dictionary gains one more field, and the counting pattern from the dictionaries lesson turns the list into a per-person summary.
Pythonchores = [ {"title": "Bins", "who": "Ana", "done": True}, {"title": "Hoover", "who": "Bo", "done": False}, {"title": "Bathroom", "who": "Ana", "done": False}, ] left = {} for chore in chores: if not chore["done"]: left[chore["who"]] = left.get(chore["who"], 0) + 1 for person in sorted(left): print(f"{person}: {left[person]} left")OutputPress Run to see what it prints.
Key takeaways
Tick each one you could explain to a friend.
0 of 5
Common questions
Guess the answer first, then open it.
Why do the Labs call functions instead of using input()?
How do I keep the tasks after the program closes?
Should the task be a class instead?
Your notes
Sign in to keep private notes alongside this lesson.
Where to go next
The quiz and the Lab live on their own pages, so this lesson stays about learning.
Finished this lesson?
Create an ID or sign in to save your progress across devices.
Check what you learned
12 questions, with an explanation after every answer. A verified attempt counts towards opening the module Lab.
Keep going in the Lab
7 tasks on this lesson, from writing code to tracing it by hand. Practice only, no XP.
Spaced review
Questions you miss in a quiz come back on a spaced schedule, so what you learned here sticks.
This lesson at a glance
The 7 lines of code this lesson uses, each with a short note. Keep it open while you do the quiz and the Lab.
What else you can search for and explore
Curious to go further? Each card opens a web search in a new tab.
References
Look things up here whenever you need the exact details.