-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20240106
73 lines (61 loc) · 2.03 KB
/
20240106
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
import json
import os
from datetime import datetime
def load_tasks():
try:
with open("tasks.json", "r") as file:
tasks = json.load(file)
except FileNotFoundError:
tasks = []
return tasks
def save_tasks(tasks):
with open("tasks.json", "w") as file:
json.dump(tasks, file)
def display_tasks(tasks):
if not tasks:
print("No tasks found.")
return
print("Tasks:")
for index, task in enumerate(tasks, start=1):
status = " [Done]" if task["done"] else " [Pending]"
print(f"{index}. {task['description']} ({task['due_date']}){status}")
def add_task(tasks):
description = input("Enter task description: ")
due_date_str = input("Enter due date (YYYY-MM-DD): ")
try:
due_date = datetime.strptime(due_date_str, "%Y-%m-%d").strftime("%Y-%m-%d")
except ValueError:
print("Invalid date format. Please use YYYY-MM-DD.")
return
new_task = {"description": description, "due_date": due_date, "done": False}
tasks.append(new_task)
print("Task added successfully.")
def complete_task(tasks):
display_tasks(tasks)
try:
index = int(input("Enter the task number to mark as done: ")) - 1
tasks[index]["done"] = True
print("Task marked as done.")
except (ValueError, IndexError):
print("Invalid task number.")
if __name__ == "__main__":
tasks = load_tasks()
while True:
print("\nTask Manager Menu:")
print("1. Display Tasks")
print("2. Add Task")
print("3. Complete Task")
print("4. Exit")
choice = input("Enter your choice (1-4): ")
if choice == "1":
display_tasks(tasks)
elif choice == "2":
add_task(tasks)
elif choice == "3":
complete_task(tasks)
elif choice == "4":
save_tasks(tasks)
print("Exiting Task Manager. Your tasks have been saved.")
break
else:
print("Invalid choice. Please enter a number between 1 and 4.")