forked from ghostmkg/programming-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo_list.py
108 lines (91 loc) · 3.3 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
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
# todo_list.py
# Class to manage the to-do list
class ToDoList:
def __init__(self):
# Initialize an empty list to store tasks
self.tasks = []
# Add a task to the list
def add_task(self, task):
self.tasks.append({"task": task, "completed": False})
print(f"Added task: '{task}'")
# Remove a task by index
def remove_task(self, index):
try:
removed_task = self.tasks.pop(index)
print(f"Removed task: '{removed_task['task']}'")
except IndexError:
print("Invalid task number.")
# Mark a task as completed
def complete_task(self, index):
try:
self.tasks[index]["completed"] = True
print(f"Marked task '{self.tasks[index]['task']}' as completed.")
except IndexError:
print("Invalid task number.")
# Edit a task's description
def edit_task(self, index, new_task):
try:
self.tasks[index]["task"] = new_task
print(f"Updated task {index + 1} to: '{new_task}'")
except IndexError:
print("Invalid task number.")
# View all tasks
def view_tasks(self):
if not self.tasks:
print("Your to-do list is empty.")
else:
for i, task in enumerate(self.tasks, start=1):
status = "Completed" if task["completed"] else "Not Completed"
print(f"{i}. {task['task']} [{status}]")
# Function to display menu
def display_menu():
print("\nTo-Do List Menu:")
print("1. View all tasks")
print("2. Add a new task")
print("3. Remove a task")
print("4. Mark a task as completed")
print("5. Edit a task")
print("6. Exit")
# Main function to run the application
def main():
todo_list = ToDoList() # Create an instance of ToDoList
while True:
display_menu()
choice = input("Enter your choice (1-6): ")
if choice == '1':
# View all tasks
todo_list.view_tasks()
elif choice == '2':
# Add a new task
task = input("Enter the task description: ")
todo_list.add_task(task)
elif choice == '3':
# Remove a task
try:
index = int(input("Enter the task number to remove: ")) - 1
todo_list.remove_task(index)
except ValueError:
print("Please enter a valid number.")
elif choice == '4':
# Mark a task as completed
try:
index = int(input("Enter the task number to mark as completed: ")) - 1
todo_list.complete_task(index)
except ValueError:
print("Please enter a valid number.")
elif choice == '5':
# Edit a task
try:
index = int(input("Enter the task number to edit: ")) - 1
new_task = input("Enter the new task description: ")
todo_list.edit_task(index, new_task)
except ValueError:
print("Please enter a valid number.")
elif choice == '6':
# Exit the application
print("Exiting the To-Do List Application. Goodbye!")
break
else:
print("Invalid choice. Please choose an option from 1 to 6.")
if __name__ == "__main__":
main()