-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo list.py
67 lines (56 loc) · 1.7 KB
/
todo list.py
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
import json
import os
TODO_FILE = "todo.json"
def load_todo():
if os.path.exists(TODO_FILE):
with open(TODO_FILE, "r") as file:
return json.load(file)
else:
return []
def save_todo(todo_list):
with open(TODO_FILE, "w") as file:
json.dump(todo_list, file)
def display_todo():
todo_list = load_todo()
if not todo_list:
print("No tasks in the to-do list.")
else:
print("To-Do List:")
for index, task in enumerate(todo_list, start=1):
print(f"{index}. {task}")
def add_task(task):
todo_list = load_todo()
todo_list.append(task)
save_todo(todo_list)
print("Task added successfully.")
def remove_task(index):
todo_list = load_todo()
if index < 1 or index > len(todo_list):
print("Invalid task index.")
return
removed_task = todo_list.pop(index - 1)
save_todo(todo_list)
print(f"Removed task: {removed_task}")
def main():
while True:
print("=== To-Do List Application ===")
print("1. Display To-Do List")
print("2. Add Task")
print("3. Remove Task")
print("4. Quit")
choice = input("Enter your choice: ")
if choice == "1":
display_todo()
elif choice == "2":
task = input("Enter the task: ")
add_task(task)
elif choice == "3":
display_todo()
index = int(input("Enter the task number to remove: "))
remove_task(index)
elif choice == "4":
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()