Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
106 lines (93 loc) · 3.2 KB
/
Copy pathmain.py
File metadata and controls
106 lines (93 loc) · 3.2 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
import json
import os
def load_tasks():
if(os.path.exists("tasks.json")):
with open("tasks.json", "r") as f:
return json.load(f)
return []
def save_tasks(tasks):
with open("tasks.json", "w") as f:
json.dump(tasks, f ,indent=2)
def add_task(tasks):
task = input("Enter task: ")
tasks.append({"id": len(tasks) + 1, "task": task, "done": False})
save_tasks(tasks)
print(f"Task added!")
def list_tasks(tasks):
if not tasks:
print("There are no tasks yet!")
return
for t in tasks:
print(f"{t['id']}. | {t["task"]} | {t["done"]}")
def complete_task(tasks):
print("A list of all tasks:")
list_tasks(tasks)
targetId = int(input("Please provide the task id:"))
for t in tasks:
if(t["id"] == targetId):
t["done"] = True
save_tasks(tasks)
print("Task has been completed!")
return
print("Task not found")
def remove_tasks(tasks):
print("Here is a list of all tasks:")
list_tasks(tasks)
print("Would you like to remove one task, all of them or all completed tasks?")
answer = input("Please type one, all or completed: ")
if(answer == "all"):
clearall()
return tasks
elif(answer == "one"):
print("Which tasks would you like to remove?")
targetId = int(input("Please type the id of the task"))
original_length = len(tasks)
tasks = [t for t in tasks if t["id"] != targetId]
if(len(tasks) == original_length):
print("Task not found!")
else:
print("Task successfully removed!")
elif(answer == "completed"):
numberofcompleted = 0
for t in tasks:
if(t["done"] == True):
numberofcompleted += 1
print(f"{numberofcompleted} completed tasks found!")
confirmation = input("Are you sure you want to remove them? yes/no ")
if(confirmation == "yes"):
tasks = [t for t in tasks if t["done"] == False]
print("All completed tasks have been removed!")
else:
print("No tasks were removed!")
else:
print("Unknown command!")
save_tasks(tasks)
def clearall():
if(os.path.exists("tasks.json")):
os.remove("tasks.json")
print("All tasks have been cleared!")
else:
print("There are no tasks to clear!")
def main():
tasks = load_tasks()
while True:
command = input("What would you like to do (add/list/complete/remove/clearall/quit)?").strip().lower()
match command:
case "add":
add_task(tasks)
case "list":
list_tasks(tasks)
case "complete":
complete_task(tasks)
case "clearall":
clearall()
tasks = load_tasks()
case "remove":
remove_tasks(tasks)
tasks = load_tasks()
case "quit":
break
case _:
print("unknown command!")
if __name__ == "__main__":
main()