1
+ # Define a class to represent a to-do list
1
2
class ToDoList:
3
+ # Initialize the ToDoList with an empty list of tasks
2
4
def __init__(self):
3
5
self.tasks = []
4
6
7
+ # Add a task to the list
5
8
def add_task(self, task):
6
9
self.tasks.append(task)
7
10
print(f"Task '{task}' added successfully!")
8
11
12
+ # Delete a task from the list if it exists
9
13
def delete_task(self, task):
10
14
if task in self.tasks:
11
15
self.tasks.remove(task)
12
16
print(f"Task '{task}' deleted successfully!")
13
17
else:
14
18
print(f"Task '{task}' not found.")
15
19
20
+ # Mark a task as completed if it exists
16
21
def mark_completed(self, task):
17
22
if task in self.tasks:
18
23
print(f"Task '{task}' marked as completed!")
19
24
else:
20
25
print(f"Task '{task}' not found.")
21
26
27
+ # Display all the tasks in the list
22
28
def display_tasks(self):
23
29
print("\nCurrent tasks:")
24
30
for i, task in enumerate(self.tasks, start=1):
25
31
print(f"{i}. {task}")
26
32
33
+ # Define the main function that will run the to-do list application
27
34
def main():
35
+ # Create an instance of the ToDoList class
28
36
todo = ToDoList()
29
37
38
+ # Loop to display the menu and process user input
30
39
while True:
31
40
print("\nMenu:")
32
41
print("1. Add Task")
@@ -35,8 +44,10 @@ def main():
35
44
print("4. Display Tasks")
36
45
print("5. Exit")
37
46
47
+ # Get the user's choice
38
48
choice = input("Enter your choice (1/2/3/4/5): ")
39
49
50
+ # Process the user's choice and call the appropriate method
40
51
if choice == '1':
41
52
task = input("Enter the task: ")
42
53
todo.add_task(task)
@@ -49,10 +60,14 @@ def main():
49
60
elif choice == '4':
50
61
todo.display_tasks()
51
62
elif choice == '5':
63
+ # Exit the application
52
64
print("Exiting the application. Have a great day!")
53
65
break
54
66
else:
67
+ # Handle invalid choices
55
68
print("Invalid choice. Please select a valid option.")
56
69
70
+ # Check if the script is being run directly and not imported
57
71
if __name__ == "__main__":
72
+ # If the script is run directly, call the main function to start the application
58
73
main()
0 commit comments