-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTO-DO Program
70 lines (61 loc) · 1.84 KB
/
TO-DO Program
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
import os
# Function to load tasks from a file
def load_tasks(file_name):
tasks = []
if os.path.exists(file_name):
with open(file_name, 'r') as file:
tasks = file.read().splitlines()
return tasks
# Function to save tasks to a file
def save_tasks(file_name, tasks):
with open(file_name, 'w') as file:
for task in tasks:
file.write(task + '\n')
# Function to display tasks
def display_tasks(tasks):
if not tasks:
print("No tasks found.")
else:
print("Your To-Do List:")
for i, task in enumerate(tasks, start=1):
print(f"{i}. {task}")
# Function to add a new task
def add_task(tasks):
task = input("Enter a new task: ")
tasks.append(task)
print(f"Task '{task}' added.")
# Function to delete a task
def delete_task(tasks):
display_tasks(tasks)
task_number = int(input("Enter the task number to delete: "))
if 1 <= task_number <= len(tasks):
removed_task = tasks.pop(task_number - 1)
print(f"Task '{removed_task}' deleted.")
else:
print("Invalid task number.")
# Main function
def main():
file_name = 'tasks.txt'
tasks = load_tasks(file_name)
while True:
print("\nTo-Do List Application")
print("1. View Tasks")
print("2. Add Task")
print("3. Delete Task")
print("4. Exit")
choice = input("Choose an option: ")
if choice == '1':
display_tasks(tasks)
elif choice == '2':
add_task(tasks)
save_tasks(file_name, tasks)
elif choice == '3':
delete_task(tasks)
save_tasks(file_name, tasks)
elif choice == '4':
print("Exiting the application.")
break
else:
print("Invalid choice. Please choose again.")
if __name__ == "__main__":
main()