-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_dict.py
More file actions
198 lines (150 loc) · 6.2 KB
/
main_dict.py
File metadata and controls
198 lines (150 loc) · 6.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import os
def clear() -> None:
if os.name == 'nt':
_ = os.system('cls')
else:
_ = os.system('clear')
def menu() -> str:
print()
print("Choose one of the following: ")
print("a. Create a new task")
print("b. Change the status of a task")
print("c. Delete a task")
print("d. Sort")
print("e. Show tasks")
print("f. Exit")
while True:
print()
main_choice: str = input("> ").lower().strip()
match main_choice:
case "a":
clear()
return "create"
case "b":
clear()
return "status"
case "c":
clear()
return "delete"
case "d":
clear()
print()
print("Sort by:")
print("g. Increasing order of priority")
print("h. Decreasing order of priority")
while True:
print()
sub_choice: str = input("> ").lower().strip()
match sub_choice:
case "g":
clear()
return "ascending"
case "h":
clear()
return "descending"
case _:
clear()
print("\nInvalid input. Try again.")
case "e":
clear()
return "show"
case "f":
clear()
exit(0)
case _:
clear()
print("\nInvalid input. Try again.")
def create(database: dict[int, dict], task_name: str, task_description: str, task_priority: int, status: str) -> None:
task: dict = {"Name": task_name, "Description": task_description, "Priority": task_priority, "Status": status}
index: int = max(database.keys()) + 1 if database.keys() else 1
database[index] = task
print("\nTask created successfully!")
return None
def task_status(database: dict[int, dict], index: int, status: str) -> None:
if index not in database:
print("The index you have entered does not exist.")
return None
current_status = database[index]["Status"]
if current_status == status:
print(f"Task {index} is already marked as '{current_status}'")
else:
database[index]["Status"] = status
print("\nTask status updated successfully!")
return None
def delete(database: dict[int, dict], index: int) -> None:
if index not in database:
print("The index you have entered does not exist.")
return None
choice: str = input(f"\nAre you sure you want to delete task {index} ({database[index]['Name']})? Enter 'yes' or 'no' \n> ")
if choice.lower() == "yes":
del database[index]
else:
return None
def sort_by(database: dict[int, dict], type: str) -> None:
reverse = True if type == "descending" else False
sorted_tasks = sorted(database.items(), key=lambda item: item[1]["Priority"], reverse=reverse)
for rank, (_, task) in enumerate(sorted_tasks, start=1):
print(f"\nRank {rank}: {task['Name']} (Priority: {task['Priority']}, Status: {task['Status']}):\n\t{task['Description']}")
def show(database: dict[int, dict]) -> None:
if not database:
print("\nNo tasks available.")
return None
for i, task in database.items():
print(f"\n{i}. {task['Name']} ({task['Status']}): \n\t{task['Description']}")
return None
def main() -> None:
db: dict[int, dict] = {}
while True:
choice: str = menu()
match choice:
case "create":
name: str = input("Enter task name: \n> ")
description: str = input("\nEnter task description: \n> ")
while True:
try:
priority: int = int(input("\nEnter task priority (Any number; higher number = more priority): \n> "))
break
except ValueError:
clear()
print("Task priority must be a number. Try again")
while True:
status: str = input("\nEnter the status of the task (complete or incomplete): \n> ").lower()
if status in ["complete", "incomplete"]:
break
else:
clear()
print("Task status must be 'complete' or 'incomplete'")
create(db, name, description, priority, status)
case "status":
while True:
try:
index: int = int(input("\nEnter index: \n> "))
break
except ValueError:
clear()
print("Index must be a number. Try again")
while True:
status: str = input("\nEnter the status of the task (complete or incomplete): \n> ").lower()
if status in ["complete", "incomplete"]:
break
else:
clear()
print("Task status must be 'complete' or 'incomplete'")
task_status(db, index, status)
case "delete":
while True:
try:
index: int = int(input("\nEnter index: \n> "))
break
except ValueError:
clear()
print("Index must be a number. Try again")
delete(db, index)
case "ascending":
sort_by(db, "ascending")
case "descending":
sort_by(db, "descending")
case "show":
show(db)
if __name__ == "__main__":
main()