-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo_app.py
More file actions
126 lines (111 loc) · 3.99 KB
/
todo_app.py
File metadata and controls
126 lines (111 loc) · 3.99 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from datetime import datetime
import json
class TodoList:
def __init__(self):
self.tasks = []
self.filename = "tasks.json"
self.id_counter = 1 # Start the counter at 1
self.load_tasks()
def add_task(self, description):
"""Add a new task"""
task = {
'id': self.id_counter, # Use the counter for the task ID
'description': description,
'completed': False,
'created_at': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
self.tasks.append(task)
self.id_counter += 1 # Increment the counter
self.save_task()
print(f"Task '{description}' added successfully!")
def view_tasks(self):
"""Display all tasks"""
if not self.tasks:
print("\nNo tasks found!")
return
print("\nYour To-Do List: ")
print("-" * 50)
for task in self.tasks:
status = "✓" if task['completed'] else " "
# 1. [ ] Build todo list app
# 2. [✓] Read book about python
print(f"{task['id']}. [{status}] {task['description']}")
print("-" * 50)
def complete_task(self, task_id):
"""Mark a task as completed"""
# 1. Loop through tasks property
# 2. check for task_id
# 3. mark task['completed'] = true
for task in self.tasks:
if task['id'] == task_id:
task['completed'] = True
self.save_task()
print(f"Task {task_id} marked as completed!")
return
print(f"Task with ID {task_id} not found!")
def delete_task(self, task_id):
"""Delete a task"""
# 1. Loop through tasks property
# 2. Check for task_id in tasks property
# 3. remove task
for task in self.tasks:
if task['id'] == task_id:
self.tasks.remove(task)
self.save_task()
print(f"Task {task_id} deleted!")
return
print(f"Task with ID {task_id} not found!")
def save_task(self):
"""Save tasks to file"""
data = {
'tasks': self.tasks,
'id_counter': self.id_counter # Save the counter
}
with open(self.filename, 'w') as file:
json.dump(data, file, indent=2)
def load_tasks(self):
"""Load tasks from file if it exists"""
try:
with open(self.filename, 'r') as file:
data = json.load(file)
self.tasks = data.get('tasks', [])
self.id_counter = data.get('id_counter', 1) # Default to 1 if not found
except FileNotFoundError:
self.tasks = []
self.id_counter = 1
def main():
todo = TodoList()
while True:
print("\n=== To-Do List Application ===")
print("1. Add task")
print("2. View task")
print("3. Complete task")
print("4. Delete task")
print("5. Exit")
choice = input("\nEnter your choice (1-5): ")
if choice == '1':
description = input("Enter task description: ")
todo.add_task(description=description)
elif choice == '2':
todo.view_tasks()
elif choice == '3':
todo.view_tasks()
try:
task_id = int(input("Enter task ID to mark as completed: "))
todo.complete_task(task_id=task_id)
except ValueError:
print("Please enter a valid task ID!")
elif choice == '4':
todo.view_tasks()
try:
task_id = int(input("Enter task ID to delete: "))
todo.delete_task(task_id=task_id)
except ValueError:
print("Please enter a valid task ID!")
elif choice == '5':
print("Thank you for using the To-Do List Application!")
break
else:
print("Invalid choice! Please try again.")
if __name__ == "__main__":
main()