-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
103 lines (81 loc) · 2.96 KB
/
main.py
File metadata and controls
103 lines (81 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
A simple Python task manager CLI application.
"""
from datetime import datetime
tasks = []
def add_task(title: str, priority: str = "medium") -> dict:
task = {
"id": len(tasks) + 1,
"title": title,
"priority": priority,
"done": False,
"created_at": datetime.now().strftime("%Y-%m-%d %H:%M"),
}
tasks.append(task)
return task
def list_tasks(show_done: bool = True):
filtered = tasks if show_done else [t for t in tasks if not t["done"]]
if not filtered:
print("No tasks found.")
return
print(f"\n{'ID':<4} {'Done':<6} {'Priority':<10} {'Title':<30} {'Created'}")
print("-" * 65)
for t in filtered:
done_mark = "✓" if t["done"] else " "
print(f"{t['id']:<4} [{done_mark}] {t['priority']:<10} {t['title']:<30} {t['created_at']}")
def complete_task(task_id: int) -> bool:
for task in tasks:
if task["id"] == task_id:
task["done"] = True
return True
return False
def delete_task(task_id: int) -> bool:
for i, task in enumerate(tasks):
if task["id"] == task_id:
tasks.pop(i)
return True
return False
def main():
print("=== Task Manager ===")
print("Commands: add, list, done, delete, quit\n")
while True:
command = input("> ").strip().lower()
if command == "quit":
print("Goodbye!")
break
elif command == "add":
title = input("Task title: ").strip()
if not title:
print("Title cannot be empty.")
continue
priority = input("Priority (low/medium/high) [medium]: ").strip().lower() or "medium"
if priority not in ("low", "medium", "high"):
print("Invalid priority. Using 'medium'.")
priority = "medium"
task = add_task(title, priority)
print(f"Added task #{task['id']}: {task['title']}")
elif command == "list":
show_done = input("Show completed tasks? (y/n) [y]: ").strip().lower()
list_tasks(show_done != "n")
elif command == "done":
try:
task_id = int(input("Task ID to mark complete: "))
if complete_task(task_id):
print(f"Task #{task_id} marked as done.")
else:
print(f"Task #{task_id} not found.")
except ValueError:
print("Please enter a valid number.")
elif command == "delete":
try:
task_id = int(input("Task ID to delete: "))
if delete_task(task_id):
print(f"Task #{task_id} deleted.")
else:
print(f"Task #{task_id} not found.")
except ValueError:
print("Please enter a valid number.")
else:
print("Unknown command. Try: add, list, done, delete, quit")
if __name__ == "__main__":
main()