Challenge: To-Do Manager
Completed
Build a small to-do manager. Read commands until the line quit:
add <task text>– add a task (the text can have spaces). PrintAdded: <task>done <number>– mark task number (from 1) as done. PrintCompleted: <task>, orNo such taskif the number is wronglist– print every task as<number>. [x] <task>if done or<number>. [ ] <task>if not, orNo tasksif empty
When you read quit, print <done>/<total> tasks done.
Input
Commands, one per line, ending with quit.
Output
As described above.
Example
Input
list add Learn loops add Build a game done 1 done 5 list quit
Output
No tasks Added: Learn loops Added: Build a game Completed: Learn loops No such task 1. [x] Learn loops 2. [ ] Build a game 1/2 tasks done
Editor
Python
Hints
Store each task as a dictionary: {"text": ..., "done": False}.
line.partition(" ") splits off the first word and keeps the rest of the text together.
Task numbers start at 1 but list indexes start at 0, and 0 or negative numbers are not valid tasks.
Solution explanation
This challenge combines lists, dictionaries, loops, functions and error handling – a real little application. Careful with done 0: tasks[-1] is valid Python, so we reject numbers below 1 ourselves.
Show solution
tasks = []
def show():
if not tasks:
print("No tasks")
for i, task in enumerate(tasks, start=1):
mark = "x" if task["done"] else " "
print(f"{i}. [{mark}] {task['text']}")
while True:
line = input()
if line == "quit":
break
command, _, rest = line.partition(" ")
if command == "add":
tasks.append({"text": rest, "done": False})
print("Added:", rest)
elif command == "done":
try:
task = tasks[int(rest) - 1]
if int(rest) < 1:
raise IndexError
except (ValueError, IndexError):
print("No such task")
else:
task["done"] = True
print("Completed:", task["text"])
elif command == "list":
show()
finished = sum(1 for task in tasks if task["done"])
print(f"{finished}/{len(tasks)} tasks done")