-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtodo.py
More file actions
70 lines (61 loc) · 2.16 KB
/
todo.py
File metadata and controls
70 lines (61 loc) · 2.16 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
class TodoApp:
def __init__(self):
self.tasks = []
def add_task(self, task):
self.tasks.append({"task": task, "completed": False})
print(f"Task '{task}' added!")
def remove_task(self, task_index):
try:
task = self.tasks.pop(task_index)
print(f"Task '{task['task']}' removed!")
except IndexError:
print("Invalid task index.")
def mark_completed(self, task_index):
try:
self.tasks[task_index]["completed"] = True
print(f"Task '{self.tasks[task_index]['task']}' marked as completed!")
except IndexError:
print("Invalid task index.")
def view_tasks(self):
if not self.tasks:
print("No tasks to show.")
else:
for idx, task in enumerate(self.tasks):
status = "Completed" if task["completed"] else "Not Completed"
print(f"{idx}. {task['task']} - {status}")
def main():
todo_app = TodoApp()
while True:
print("\n----- Simple To-Do List -----")
print("1. Add Task")
print("2. Remove Task")
print("3. Mark Task as Completed")
print("4. View Tasks")
print("5. Exit")
choice = input("Choose an option: ")
if choice == '1':
task = input("Enter task: ")
todo_app.add_task(task)
elif choice == '2':
todo_app.view_tasks()
try:
task_index = int(input("Enter task index to remove: "))
todo_app.remove_task(task_index)
except ValueError:
print("Please enter a valid index.")
elif choice == '3':
todo_app.view_tasks()
try:
task_index = int(input("Enter task index to mark as completed: "))
todo_app.mark_completed(task_index)
except ValueError:
print("Please enter a valid index.")
elif choice == '4':
todo_app.view_tasks()
elif choice == '5':
print("Goodbye!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()