-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
107 lines (84 loc) · 2.71 KB
/
Copy pathmain.py
File metadata and controls
107 lines (84 loc) · 2.71 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
104
105
106
107
import json
import os
TODO_FILE = "todo_list.json"
def load_tasks():
if not os.path.exists(TODO_FILE):
return []
try:
with open(TODO_FILE, "r") as f: # Modified to fit file context
return json.load(f)
except json.JSONDecodeError:
print("Warning: Corrupted save file. Starting with an empty list.")
return []
def save_tasks(tasks):
with open(TODO_FILE, "w") as f:
json.dump(tasks, f, indent=4)
def show_tasks(tasks):
if not tasks:
print("\nYour to-do list is empty.")
return
print("\n--- Current Tasks ---")
for i, task in enumerate(tasks, 1):
status = "✓" if task["completed"] else " "
print(f"{i}. [{status}] {task['title']}")
def add_task(tasks):
title = input("\nEnter task description: ").strip()
if title:
tasks.append({"title": title, "completed": False})
save_tasks(tasks)
print(f"Added: '{title}'")
else:
print("Task description cannot be empty.")
def toggle_task(tasks):
show_tasks(tasks)
if not tasks:
return
try:
choice = int(input("\nEnter the number of the task to toggle: "))
if 1 <= choice <= len(tasks):
tasks[choice - 1]["completed"] = not tasks[choice - 1]["completed"]
save_tasks(tasks)
print("Task status updated.")
else:
print("Invalid task number.")
except ValueError:
print("Please enter a valid number.")
def delete_task(tasks):
show_tasks(tasks)
if not tasks:
return
try:
choice = int(input("\nEnter the number of the task to delete: "))
if 1 <= choice <= len(tasks):
removed = tasks.pop(choice - 1)
save_tasks(tasks)
print(f"Deleted: '{removed['title']}'")
else:
print("Invalid task number.")
except ValueError:
print("Please enter a valid number.")
def main():
tasks = load_tasks()
while True:
print("\n=== To-Do List ===")
print("1. View Tasks")
print("2. Add Task")
print("3. Toggle Complete")
print("4. Delete Task")
print("5. Exit")
choice = input("Choose an option (1-5): ").strip()
if choice == "1":
show_tasks(tasks)
elif choice == "2":
add_task(tasks)
elif choice == "3":
toggle_task(tasks)
elif choice == "4":
delete_task(tasks)
elif choice == "5":
print("Goodbye!")
break
else:
print("Invalid choice, try again.")
if __name__ == "__main__":
main()