-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodos.py
More file actions
60 lines (47 loc) · 2.33 KB
/
todos.py
File metadata and controls
60 lines (47 loc) · 2.33 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
import myfunctions # from backend.py
import time
now = time.strftime("%b %d, %Y %H:%M:%S")
print(f"It is {now}")
while True:
user_action = input("Type add, show or edit or complete or exit: ").strip() + "\n"
# ADD
if user_action.startswith('add'):
todo = user_action[4:]
todos = myfunctions.get_todos() # OR get_todos('files/NEWTODOS.txt') : to override the default value | make sure the overriden file exists
todos.append(todo)
myfunctions.write_todos(todos) # to write the contents of todos to the file | the filepath is already defined in function so no need to mention it again
# SHOW
elif user_action.startswith('show') or user_action.startswith('display'):
todos = myfunctions.get_todos()
for index, item in enumerate(todos):
row = f"{index}-{item}"
print(row.strip('\n'))
# EDIT
elif user_action.startswith('edit'):
try: # try: runs the actual program
number = int(user_action[5:])
todos = myfunctions.get_todos()
new_todo: str = input("Enter new todo: ") + "\n"
todos[number] = new_todo
myfunctions.write_todos(todos) # OR myfunctions.write_todos(filepath='files/todos.txt', todos_arg=todos) --> here order doesn't matter as argument name is mentioned.
except ValueError: # short for exception: except: to customise the output in case of error | Here ValueError comes up when the parameter pass to edit command is not an integer as per this case
print("Command Invalid. Valid command is : edit <number>")
continue # continue to run the while loop | opposite of break
except IndexError: # If the number provided is not in the range of Index
print(f"There's no item with number {number}")
continue
# COMPLETE
elif user_action.startswith('complete'):
try:
number = int(user_action[9:])
todos = myfunctions.get_todos()
todos.pop(number)
myfunctions.write_todos(todos)
except IndexError: # If the number provided is not in the range of Index
print(f"There's no item with number {number}")
# EXIT
elif user_action.startswith('exit'):
break
else:
print("Hey, you entered an unknown command")
print("Bye!")