-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1753974000.py
More file actions
96 lines (77 loc) · 2.53 KB
/
1753974000.py
File metadata and controls
96 lines (77 loc) · 2.53 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
import json
import os
TODO_FILE = "todo_list.json"
def load_tasks():
if os.path.exists(TODO_FILE):
with open(TODO_FILE, "r") as file:
try:
return json.load(file)
except json.JSONDecodeError:
return []
return []
def save_tasks(tasks):
with open(TODO_FILE, "w") as file:
json.dump(tasks, file, indent=4)
def add_task(tasks):
task_description = input("Enter the task description: ")
tasks.append({"description": task_description, "completed": False})
save_tasks(tasks)
print("Task added successfully.")
def view_tasks(tasks):
if not tasks:
print("No tasks in the list.")
return
print("\n--- To-Do List ---")
for i, task in enumerate(tasks):
status = "\u2713" if task["completed"] else " "
print(f"{i + 1}. [{status}] {task['description']}")
print("------------------\n")
def mark_task_complete(tasks):
view_tasks(tasks)
try:
task_num = int(input("Enter the task number to mark as complete: "))
if 1 <= task_num <= len(tasks):
tasks[task_num - 1]["completed"] = True
save_tasks(tasks)
print("Task marked as complete.")
else:
print("Invalid task number.")
except ValueError:
print("Invalid input. Please enter a number.")
def delete_task(tasks):
view_tasks(tasks)
try:
task_num = int(input("Enter the task number to delete: "))
if 1 <= task_num <= len(tasks):
removed_task = tasks.pop(task_num - 1)
save_tasks(tasks)
print(f"Task '{removed_task['description']}' deleted.")
else:
print("Invalid task number.")
except ValueError:
print("Invalid input. Please enter a number.")
def main():
tasks = load_tasks()
while True:
print("\nToDo App Menu:")
print("1. Add a task")
print("2. View all tasks")
print("3. Mark a task as complete")
print("4. Delete a task")
print("5. Exit")
choice = input("Enter your choice (1-5): ")
if choice == "1":
add_task(tasks)
elif choice == "2":
view_tasks(tasks)
elif choice == "3":
mark_task_complete(tasks)
elif choice == "4":
delete_task(tasks)
elif choice == "5":
print("Exiting ToDo App. Goodbye!")
break
else:
print("Invalid choice. Please enter a number between 1 and 5.")
if __name__ == "__main__":
main()