-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTo-Do_List.py
69 lines (59 loc) · 2.08 KB
/
To-Do_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
# For adding a task
def add_task(task, filename="tasks.txt"):
with open(filename, "a") as file:
file.write(task + "\n")
print(f"Task '{task}' added.")
# For viewing the added tasks
def view_tasks(filename="tasks.txt"):
try:
with open(filename, "r") as file:
tasks = file.readlines()
if tasks:
print("Your To-Do List:")
for i, task in enumerate(tasks, 1):
print(f"{i}. {task.strip()}")
else:
print("Your To-Do List is empty.")
except FileNotFoundError:
print("No tasks found. Start by adding a new task.")
# For delete a task
def delete_task(task_number, filename="tasks.txt"):
try:
with open(filename, "r") as file:
tasks = file.readlines()
if 0 < task_number <= len(tasks):
task = tasks.pop(task_number - 1).strip()
with open(filename, "w") as file:
file.writelines(tasks)
print(f"Task '{task}' deleted.")
else:
print("Invalid task number.")
except FileNotFoundError:
print("No tasks found. Start by adding a new task.")
# Main function for implement the CLI logic
def main():
while True:
print("\nSimple To-Do List CLI")
print("1. Add Task")
print("2. View Tasks")
print("3. Delete Task")
print("4. Exit")
choice = input("Enter your choice: ")
if choice == "1":
task = input("Enter the task: ")
add_task(task)
elif choice == "2":
view_tasks()
elif choice == "3":
try:
task_number = int(input("Enter the task number to delete: "))
delete_task(task_number)
except ValueError:
print("Please enter a valid number.")
elif choice == "4":
print("Thank you for using !")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()