-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.py
90 lines (82 loc) · 2.66 KB
/
todo.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
import json
import os
import time
# Function to clear the console
def clear_console():
os.system('cls' if os.name == 'nt' else 'clear')
# Function to display the Todo list
def display_todo_list(todo_list):
clear_console()
if not todo_list:
print("No items in the Todo list.")
else:
print("Todo List:")
for i, item in enumerate(todo_list, start=1):
print(f"{i}. {item['task']}")
# Function to add a new task to the Todo list
def add_task(todo_list, task):
clear_console()
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
todo_list.append({"task": task, "timestamp": timestamp})
print("Task added successfully.")
# Function to remove a task from the Todo list
def remove_task(todo_list, task_number):
clear_console()
if not todo_list:
return
try:
if task_number < 1 or task_number > len(todo_list):
print("Invalid task number.")
else:
todo_list.pop(task_number - 1)
print("Task removed successfully.")
except ValueError:
print("Invalid task number.")
# Function to save the Todo list to a JSON file
def save_todo_list(todo_list):
with open("todo_list.json", "w") as file:
json.dump(todo_list, file)
# Function to load the Todo list from a JSON file
def load_todo_list():
if os.path.exists("todo_list.json"):
with open("todo_list.json", "r") as file:
todo_list = json.load(file)
return todo_list
else:
return []
# Menu function
def main():
todo_list = load_todo_list()
while True:
clear_console()
print("=====================")
print("Todo List App")
print("1. Display Todo List")
print("2. Add Task")
print("3. Remove Task")
print("4. Exit")
print("=====================")
print()
print(f"Debug Info: {type(todo_list)}")
print()
choice = input("Enter your choice (1-4): ")
if choice == "1":
display_todo_list(todo_list)
input("Press Enter to continue...")
elif choice == "2":
task = input("Enter task: ")
add_task(todo_list, task)
save_todo_list(todo_list)
input("Press Enter to continue...")
elif choice == "3":
display_todo_list(todo_list)
task_number = int(input("Enter task number to remove: "))
remove_task(todo_list, task_number)
save_todo_list(todo_list)
input("Press Enter to continue...")
elif choice == "4":
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()