-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
138 lines (128 loc) · 4.43 KB
/
project.py
File metadata and controls
138 lines (128 loc) · 4.43 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
from tabulate import tabulate
import os
meun = [
[1, "Add a new task"],
[2, "Remove a task"],
[3, "Modify existing tasks"],
[4, "View tasks"],
[9, "Exit"]
]
headers = ["Index", "Choices"]
header1 = ["Index", "Tasks"]
choices = [1, 2, 3, 4, 9]
tasks = []
showtasks = []
def main():
os.system('clear')
import_file()
while True:
choice = get_choice()
if choice == 1:
new_task()
elif choice == 2:
remove_task()
elif choice == 3:
modify_tasks()
elif choice == 4:
os.system('clear')
view_tasks()
print("------------------------------------------------")
print("The tasks is showed.")
print("------------------------------------------------")
else:
save_tasks()
print("------------------------------------------------")
print("Thank you for your using! All the things are saved.")
print("------------------------------------------------")
break
def import_file():
try:
with open("tasks.txt", "r") as file:
global tasks
tasks = [line.strip() for line in file.readlines() if line.strip()]
print("Tasks imported successfully!")
except FileNotFoundError:
print("No tasks file found. Starting with an empty task list.")
except Exception as e:
print(f"An error occurred while importing tasks: {e}")
def get_choice():
while True:
try:
print(tabulate(meun, headers, tablefmt="heavy_grid"))
choice = input("What is your choice: ")
choice = int(choice)
if choice in choices:
return choice
else:
raise ValueError
except:
os.system('clear')
print("------------------------------------------------")
print("Invalid input, please input again!")
print("------------------------------------------------")
def new_task():
os.system('clear')
newtask = input("What is your task: ")
tasks.append(newtask.strip())
view_tasks()
print("------------------------------------------------")
print("The task is added!")
print("------------------------------------------------")
def remove_task():
os.system('clear')
while True:
try:
view_tasks()
removetaskindex = input("Which task you want to remove: ")
removetaskindex = int(removetaskindex)
if removetaskindex < int(len(tasks)):
tasks.remove(tasks[removetaskindex])
print("------------------------------------------------")
print("The task is removed!")
print("------------------------------------------------")
break
else:
raise ValueError
except:
os.system('clear')
print("------------------------------------------------")
print("Invalid input, please input again!")
print("------------------------------------------------")
def modify_tasks():
os.system('clear')
while True:
try:
view_tasks()
index = input("Which task you want to modify: ")
index = int(index)
if index < len(tasks):
tasks[index] = input("Please enter your modification: ")
print("------------------------------------------------")
print("The task is modified!")
print("------------------------------------------------")
break
else:
raise ValueError
except:
os.system('clear')
print("------------------------------------------------")
print("Invalid input, please input again!")
print("------------------------------------------------")
def view_tasks():
print("Here is the task info:")
i = 0
for i in range(len(tasks)):
showtasks.append([i,tasks[i]])
i += 1
print(tabulate(showtasks, header1, tablefmt="heavy_grid"))
showtasks.clear()
def save_tasks():
try:
with open('tasks.txt', 'w') as file:
for task in tasks:
file.write(f'{task}\n')
print('Tasks saved successfully!')
except Exception as e:
print(f'An error occurred while saving tasks: {e}')
if __name__ == "__main__":
main()