TaskManager manages the collection.
Task stores data for one task. TaskManager owns the list, so methods that search, change, or remove items belong on TaskManager.
def get_task(self, task_id: str) -> Task:
for task in self.tasks:
if task.task_id == task_id:
return task
raise ValueError("Task was not found.")The loop examines each existing Task. A matching task_id returns the same object from the list. A missing ID raises ValueError, so every caller gets one clear, predictable result.
This lesson uses one policy: duplicate IDs are rejected with ValueError, and a missing ID also raises ValueError. Repeating complete_task() is safe: the task simply remains completed.
Build on the existing method: add_task() remains the starting point. Before appending, it should check whether get_task() can already find the same ID and reject an ambiguous duplicate.
task = Task("task-1", "Read the plan")
manager = TaskManager()
manager.add_task(task)
task.completed = True
print(manager.tasks[0].completed)The list stores a reference to the Task object, not a separate copy. When completed becomes True, the Task reached through manager.tasks[0] shows the same changed state.
def complete_task(self, task_id: str) -> Task:
task = self.get_task(task_id)
task.completed = True
return taskA small method can reuse get_task() instead of repeating the search loop. You will write the remaining methods yourself in the local project.
Choose predictable behavior
- View and findLet
list_tasks()return the task list andget_task(task_id)return one matching Task. - Complete safelyLet
complete_task()setcompletedto True. Calling it again should not duplicate or break the task. - Delete deliberatelyLet
delete_task()remove the matching Task after it is found. A later lookup for that ID should raise ValueError.
Common task-management mistakes
- Allowing duplicate task IDsTwo different tasks with one ID make get_task() ambiguous. Reject the duplicate before changing the list.
- Returning a hidden failureDo not silently return an unrelated Task for a missing ID. Use the same clear ValueError policy in each operation.
