-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodos.py
More file actions
94 lines (67 loc) · 1.98 KB
/
Copy pathtodos.py
File metadata and controls
94 lines (67 loc) · 1.98 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
import json
FILE_NAME = "todos.json"
def load_tasks():
try:
with open(FILE_NAME, "r", encoding="utf-8") as file:
return json.load(file)
except FileNotFoundError:
return []
except json.JSONDecodeError:
return []
def save_tasks(tasks):
with open(FILE_NAME, "w", encoding="utf-8") as file:
json.dump(tasks, file, indent=2, ensure_ascii=False)
def add_task():
topic = input("Enter the task topic: ").strip()
if not topic:
print("Task topic cannot be empty.")
return
tasks = load_tasks()
if tasks:
new_id = tasks[-1]["id"] + 1
else:
new_id = 1
new_task = {
"id": new_id,
"topic": topic,
"completed": False
}
tasks.append(new_task)
save_tasks(tasks)
print(f"Task '{topic}' added successfully.")
def list_tasks():
tasks = load_tasks()
if not tasks:
print("No tasks found.")
return
for task in tasks:
status = "x" if task["completed"] else " "
print(f"[{status}] {task['id']}. {task['topic']}")
def complete_task():
tasks = load_tasks()
list_tasks()
selected_id = input("Enter the ID of the task to mark as completed: ")
found = False
for task in tasks:
if str(task["id"]) == selected_id:
task["completed"] = True
found = True
break
if found:
save_tasks(tasks)
print(f"Task ID {selected_id} marked as completed.")
else:
print(f"Task ID {selected_id} not found.")
def remove_task():
tasks = load_tasks()
if not tasks:
print("No tasks found.")
return
list_tasks()
selected_id = input("Enter the ID of the task to remove: ")
new_list = [task for task in tasks if str(task["id"]) != selected_id]
if len(new_list) == len(tasks):
print(f"Task ID {selected_id} not found.")
else:
save_tasks(new_list)
print(f"Task ID {selected_id} removed successfully.")