-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoDoList.py
103 lines (72 loc) · 2.45 KB
/
toDoList.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
s_no = 1 #global var
class to_do_list:
def __init__(self):
self.list = {}
def addNew(self, task):
global s_no
self.list[s_no] = {"Task": task}
s_no+=1
def viewTask(self):
print("\nTo Do List:-")
if not self.list:
print("<Empty>")
return
for s_no, details in self.list.items():
print(f"Task {s_no}: {details['Task']}")
def updateTask(self, num, task):
num = int(num)
for s_no, details in self.list.items():
if s_no == num:
self.list[s_no]["Task"] = task
print(f"Task {num} is updated")
break
def deleteTask(self, num):
global s_no
if num in self.list:
self.list.pop(num)
# Rearrange the remaining tasks
for i in range(num, s_no - 1):
self.list[i] = self.list.pop(i + 1)
s_no -= 1
print(f"Task {num} is deleted.")
else:
print(f"Task {num} not found.")
#Main()_Body
todo = to_do_list()
while True:
todo.viewTask()
print("\n\tMAIN MENU")
print("1. Add New Task")
print("2. Update a Task")
print("3. Delete a Task")
print("4. Exit")
choice = input("\nEnter your choice: ")
if choice == "1":
task= input("Enter the task: ")
todo.addNew(task)
elif choice == "2":
while True:
num=int(input("Enter the task no.: "))
if num>=s_no or num<=0 :
print(f"Task {num} not found.\n")
else:
task=input("Enter the updated task: ")
todo.updateTask(num, task)
break
elif choice == "3":
while True:
num=int(input("Enter the task no.: "))
todo.deleteTask(num)
break
elif choice == "4":
while True:
choice2 = input("Want to Exit (Y/N): ")
if choice2 == 'y' or choice2 == 'Y':
print("\nExiting Program.")
exit()
elif choice2 == 'n' or choice2 == 'N':
break
else:
print("Invalid choice. Please select a valid option.")
else:
print("Invalid choice. Please select a valid option.")