-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_scheduler.py
More file actions
78 lines (69 loc) · 2.12 KB
/
Copy pathtask_scheduler.py
File metadata and controls
78 lines (69 loc) · 2.12 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
import json
import os
from datetime import datetime
TASKS_FILE = "tasks.json"
def load_tasks():
if os.path.exists(TASKS_FILE):
with open(TASKS_FILE, "r") as f:
return json.load(f)
return []
def save_tasks(tasks):
with open(TASKS_FILE, "w") as f:
json.dump(tasks, f, indent=4)
def add_task(tasks):
description = input("Enter task description: ").strip()
if not description:
print("Description cannot be empty!")
return
due_date = input("Enter due date (YYYY-MM-DD): ").strip()
try:
datetime.strptime(due_date, "%Y-%m-%d")
except ValueError:
print("Invalid date format! Use YYYY-MM-DD.")
return
tasks.append({"description": description, "due_date": due_date, "completed": False})
save_tasks(tasks)
print("Task added successfully!")
def list_tasks(tasks):
if not tasks:
print("No tasks found.")
return
for i, task in enumerate(tasks, 1):
status = "✓" if task["completed"] else "✗"
print(f"{i}. {task['description']} (Due: {task['due_date']}) [{status}]")
def complete_task(tasks):
list_tasks(tasks)
if not tasks:
return
try:
index = int(input("Enter task number to complete: ")) - 1
if 0 <= index < len(tasks):
tasks[index]["completed"] = True
save_tasks(tasks)
print("Task marked as completed!")
else:
print("Invalid task number!")
except ValueError:
print("Enter a valid number!")
def main():
tasks = load_tasks()
while True:
print("\nTask Scheduler")
print("1. Add Task")
print("2. List Tasks")
print("3 W. Complete Task")
print("4. Exit")
choice = input("Choose an option (1-4): ").strip()
if choice == "1":
add_task(tasks)
elif choice == "2":
list_tasks(tasks)
elif choice == "3":
complete_task(tasks)
elif choice == "4":
print("Goodbye!")
break
else:
print("Invalid choice! Try again.")
if __name__ == "__main__":
main()