Save and Load JSON
Completed
Read n lines task done (done is yes/no). Save the list of tasks as JSON to todo.json (each task a dict with task and done as a boolean). Load it back and print [x] task or [ ] task for each, then the number of tasks still to do.
Input
Line 1: n. Then n lines.
Output
n + 1 lines.
Example
Input
3 shop yes code no sleep no
Output
[x] shop [ ] code [ ] sleep 2
Editor
Python
Hints
json.dump(data, file) writes JSON; json.load(file) reads it.
Solution explanation
JSON files are a simple way to save program state between runs.
Show solution
import json
tasks = []
for _ in range(int(input())):
name, done = input().split()
tasks.append({"task": name, "done": done == "yes"})
with open("todo.json", "w") as f:
json.dump(tasks, f)
with open("todo.json") as f:
loaded = json.load(f)
for t in loaded:
print(f"[{'x' if t['done'] else ' '}] {t['task']}")
print(sum(not t["done"] for t in loaded))