-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTo-do-list-game.py
More file actions
83 lines (70 loc) · 2.15 KB
/
To-do-list-game.py
File metadata and controls
83 lines (70 loc) · 2.15 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
#Roadmap
#Load existing items
#1. create a new item
#2. list items
#3. mark item as complete
#4. save items
import json #java_script_object_notation
file_name = "todo_list.json"
def load_tasks():
try:
with open(file_name, "r") as file:
return json.load(file)
except:
return{"tasks":[]}
def save_tasks(tasks):
try:
with open(file_name, "w") as file:
json.dump(tasks,file)
except:
print("Failed to save.")
def view_tasks(tasks):
task_list = tasks["tasks"]
if len(task_list) == 0:
print("No tasks to display.")
else:
print("Your To-Do List: ")
for idx, task in enumerate(task_list):
status = "[Completed]" if task["complete"] else "[Pending]"
print(f"{idx + 1}. {task['description']} | {status}")
def create_tasks(tasks):
description = input("Enter the task description: ").strip()
if description:
tasks["tasks"].append({"description": description, "complete": False})
save_tasks(tasks)
print("Task added.")
else:
print("Descripiton cannot be empty.")
def mark_task_complete(tasks):
view_tasks(tasks)
try:
task_number = int(input("Enter the task number to mark as complete: ").strip())
if 1 <= task_number <= len(tasks["tasks"]):
tasks["tasks"][task_number - 1]["complete"] = True
save_tasks(tasks)
print("Task marked as complete.")
else:
print("Invalid task number.")
except:
print("Enter a valid number.")
def main():
tasks = load_tasks()
while True:
print("\nTo-Do List Manager")
print("1. View Tasks")
print("2. Add Task")
print("3. Complete Task")
print("4. Exit")
choice = input("Enter your choice: ").strip()
if choice == "1":
view_tasks(tasks)
elif choice == "2":
create_tasks(tasks)
elif choice == "3":
mark_task_complete(tasks)
elif choice == "4":
print("Goodbye!")
break
else:
print("Invalid choice. Please try again.")
main()