…
…
Write functions that add, complete and delete tasks; process commands, catch errors and keep the list in a JSON file.
In this project you will build a small to-do list program. Each task is a dictionary: its number (id), its title and whether it is done. You will keep the tasks in a list, write functions to add, list, complete and delete them, process text commands and save the list to a JSON file.
Lessons used: functions (14–16), string methods (21), lists (23–24), dictionaries and lists of records (26–28), comprehensions (30), try, except, raise (32), files and JSON (33, 35).
The functions take the list as a parameter, so you can try the same function on different lists. Each cell builds its own example list, so running it again does not change the result.
What you'll do
Required topics
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The function add_task(tasks, title) should add a new task dictionary to the list and return the task's number. The new number is one more than the largest number in the list; if the list is empty it is 1. Collect the numbers in a list with [t["id"] for t in tasks] (lesson 30) and take the largest with max (lesson 24). Because max raises an error on an empty list, first ask whether the list has items: an empty list counts as false in a condition (lesson 8).
A new task always starts as not done (False).
This step's code cell is for members.Sign up free
list_tasks(tasks) should return a list with one line of text for each task. Tasks that are done start with [x], tasks that are not done start with [ ]:
Output[ ] 1. Prepare the slides
[x] 2. Buy groceriesThe function does not print; it returns the lines, and the calling code does the printing. This makes the function easier to test.
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
This step's code cell is for members.Sign up free
complete_task(tasks, task_id) should mark the task with the given number as done and return its title. If there is no such task it should not pass silently; it should raise an error: raise ValueError("Task 7 not found").
The function does not decide what to do about the error; the code that calls it does: the try / except below catches the error and prints its message.
This step's code cell is for members.Sign up free
delete_task(tasks, task_id) should remove the task from the list and return its title; if the task does not exist it should raise ValueError, as complete_task does.
enumerate gives both the position and the item (lesson 24). When you find the task, tasks.pop(i) removes it from the list and returns it. Because you leave the loop at once with return, changing the list you are looping over is not a problem here.
This step's code cell is for members.Sign up free
The program will process text commands typed by the user. Because the browser cannot read keyboard input, the commands are in a ready-made list:
| Command | What it does |
|---|---|
add <title> | adds a new task |
done <number> | completes the task |
delete <number> | deletes the task |
list | prints all tasks |
command.split() splits the text at spaces: "add Pay the bill" → ["add", "Pay", "the", "bill"]. The first part is the action; because the title can have several words, you join the remaining parts again with " ".join(parts[1:]) (lessons 21 and 23): "Pay the bill".
If text is typed instead of a number by mistake, parse_number raises a clear error. text.isdigit() returns True if the text consists only of digits: "12".isdigit() → True, "abc".isdigit() → False. A single except catches all the errors; the program does not stop and moves on to the next command.
This step's code cell is for members.Sign up free
So that the list is not lost when the program closes, you will write the tasks to a JSON file. json.dump writes the list to the file; ensure_ascii=False keeps accented letters readable, and indent=2 makes the file look tidy.
load_tasks should not crash the program if the file does not exist; it should return an empty list. Which error does Python raise when a file is not found?
This step's code cell is for members.Sign up free
Finally, write a summary line under the list: how many tasks are done and what percentage that is.
Filter the done tasks with a list comprehension: [t for t in tasks if t["done"]]. To show the percentage rounded to a whole number, use :.0f.
This step's code cell is for members.Sign up free