-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.py
More file actions
71 lines (64 loc) · 1.83 KB
/
todo.py
File metadata and controls
71 lines (64 loc) · 1.83 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
FILENAME = "tasks.txt"
def load_tasks():
try:
with open(FILENAME, "r") as f:
tasks = [line.strip() for line in f.readlines()]
except FileNotFoundError:
tasks = []
return tasks
def save_tasks(tasks):
with open(FILENAME, "w") as f:
for task in tasks:
f.write(task + "\n")
def add_task(tasks):
task = input("Enter new task: ").strip()
if task:
tasks.append(task)
save_tasks(tasks)
print(f"Task '{task}' added!")
else:
print("Task cannot be empty.")
def view_tasks(tasks):
if not tasks:
print("No tasks found.")
else:
print("\nYour Tasks:")
for i, task in enumerate(tasks, 1):
print(f"{i}. {task}")
print()
def remove_task(tasks):
view_tasks(tasks)
if not tasks:
return
try:
num = int(input("Enter task number to remove: "))
if 1 <= num <= len(tasks):
removed = tasks.pop(num - 1)
save_tasks(tasks)
print(f"Removed '{removed}'.")
else:
print("Invalid task number.")
except ValueError:
print("Please enter a valid number.")
def main():
tasks = load_tasks()
while True:
print("\n--- To-Do List Menu ---")
print("1. View Tasks")
print("2. Add Task")
print("3. Remove Task")
print("4. Exit")
choice = input("Choose an option: ").strip()
if choice == "1":
view_tasks(tasks)
elif choice == "2":
add_task(tasks)
elif choice == "3":
remove_task(tasks)
elif choice == "4":
print("Goodbye!")
break
else:
print("Invalid option, try again.")
if __name__ == "__main__":
main()