-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathto_do_list.py
More file actions
74 lines (58 loc) · 1.76 KB
/
Copy pathto_do_list.py
File metadata and controls
74 lines (58 loc) · 1.76 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
import json
TASKS_FILE = "tasks.json"
def load_tasks():
try:
with open(TASKS_FILE, "r") as file:
return json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
return []
def save_tasks(tasks):
with open(TASKS_FILE, "w") as file:
json.dump(tasks, file, indent=4)
def add_task(task):
tasks = load_tasks()
tasks.append({"task": task, "done": False})
save_tasks(tasks)
print("Task added!")
def list_tasks():
tasks = load_tasks()
if not tasks:
print("No tasks available.")
for i, task in enumerate(tasks, 1):
status = "[Done]" if task["done"] else "[Pending]"
print(f"{i}. {status} {task['task']}")
def mark_done(task_number):
tasks = load_tasks()
if 0 < task_number <= len(tasks):
tasks[task_number - 1]["done"] = True
save_tasks(tasks)
print("Task marked as done!")
else:
print("Invalid task number.")
def main():
while True:
print("\nTo-Do List Menu:")
print("1. Add Task")
print("2. View Tasks")
print("3. Mark Task as Done")
print("4. Exit")
choice = input("Choose an option: ")
if choice == "1":
task = input("Enter task: ")
add_task(task)
elif choice == "2":
list_tasks()
elif choice == "3":
list_tasks()
try:
task_number = int(input("Enter task number to mark as done: "))
mark_done(task_number)
except ValueError:
print("Please enter a valid number.")
elif choice == "4":
print("Goodbye!")
break
else:
print("Invalid choice. Try again.")
if __name__ == "__main__":
main()