forked from souravjain540/Basic-Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToDoList.py
More file actions
88 lines (79 loc) · 2.51 KB
/
ToDoList.py
File metadata and controls
88 lines (79 loc) · 2.51 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
import os
# Function to load tasks from a file
def load_tasks(filename="tasks.txt"):
if os.path.exists(filename):
with open(filename, 'r') as file:
tasks = [line.strip() for line in file]
else:
tasks = []
return tasks
# Function to save tasks to a file
def save_tasks(tasks, filename="tasks.txt"):
with open(filename, 'w') as file:
for task in tasks:
file.write(task + '\n')
# 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 view all tasks
def view_tasks(tasks):
if tasks:
print("\nYour Tasks:")
for i, task in enumerate(tasks, 1):
print(f"{i}. {task}")
else:
print("\nNo tasks to show.")
# Function to remove a task
def remove_task(tasks):
view_tasks(tasks)
try:
task_num = int(input("Enter the number of the task to remove: ")) - 1
if 0 <= task_num < len(tasks):
removed_task = tasks.pop(task_num)
print(f"Task '{removed_task}' removed.")
else:
print("Invalid task number.")
except ValueError:
print("Please enter a valid number.")
# Function to mark a task as complete
def mark_task_complete(tasks):
view_tasks(tasks)
try:
task_num = int(input("Enter the number of the task to mark as complete: ")) - 1
if 0 <= task_num < len(tasks):
tasks[task_num] = tasks[task_num] + " (Completed)"
print(f"Task '{tasks[task_num]}' marked as complete.")
else:
print("Invalid task number.")
except ValueError:
print("Please enter a valid number.")
# Main function to run the application
def main():
tasks = load_tasks()
while True:
print("\nTo-Do List Application")
print("1. Add Task")
print("2. View Tasks")
print("3. Remove Task")
print("4. Mark Task as Complete")
print("5. Save and Exit")
choice = input("Choose an option: ")
if choice == '1':
add_task(tasks)
elif choice == '2':
view_tasks(tasks)
elif choice == '3':
remove_task(tasks)
elif choice == '4':
mark_task_complete(tasks)
elif choice == '5':
save_tasks(tasks)
print("Tasks saved. Exiting...")
break
else:
print("Invalid choice. Please try again.")
# Entry point of the application
if __name__ == "__main__":
main()