Basic Task Manager in Python
class Task:
def __init__(self, title, description):
self.title = title
self.description = description
self.completed = False
def complete(self):
self.completed = True
def __str__(self):
status = "✓" if self.completed else "✗"
return f"[{status}] {self.title}: {self.description}"
class TaskManager:
def __init__(self):
self.tasks = []
def add_task(self, title, description):
task = Task(title, description)
self.tasks.append(task)
print(f"Task '{title}' added.")
def list_tasks(self):
if not self.tasks:
print("No tasks available.")
for i, task in enumerate(self.tasks, start=1):
print(f"{i}. {task}")
def complete_task(self, index):
if 0 <= index < len(self.tasks):
self.tasks[index].complete()
print(f"Task '{self.tasks[index].title}' completed.")
else:
print("Invalid task index.")
def delete_task(self, index):
if 0 <= index < len(self.tasks):
removed_task = self.tasks.pop(index)
print(f"Task '{removed_task.title}' deleted.")
else:
print("Invalid task index.")
def main():
manager = TaskManager()
while True:
print("\nTask Manager")
print("1. Add Task")
print("2. List Tasks")
print("3. Complete Task")
print("4. Delete Task")
print("5. Exit")
choice = input("Choose an option: ")
if choice == '1':
title = input("Enter task title: ")
description = input("Enter task description: ")
manager.add_task(title, description)
elif choice == '2':
manager.list_tasks()
elif choice == '3':
index = int(input("Enter task index to complete: ")) - 1
manager.complete_task(index)
elif choice == '4':
index = int(input("Enter task index to delete: ")) - 1
manager.delete_task(index)
elif choice == '5':
print("Exiting Task Manager.")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
How to Use
- Add Task: Enter a title and description for the task.
- List Tasks: View all tasks with their completion status.
- Complete Task: Mark a task as completed by its index.
- Delete Task: Remove a task by its index.
- Exit: Quit the application.
Running the Code
- Save the code in a file named
task_manager.py
.
- Run the script using Python:
python task_manager.py
Leave a Reply