Repository navigation
Expand file tree
/
Copy pathtask_manager.py
More file actions
207 lines (155 loc) · 4.49 KB
/
Copy pathtask_manager.py
File metadata and controls
207 lines (155 loc) · 4.49 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""
Task Manager Application
------------------------
A simple console-based task manager using JSON for data storage.
Features:
- Add tasks (with optional notes)
- View all tasks
- Mark tasks as completed
- Rename tasks
- Remove tasks
- Persistent storage using JSON
Author: Ahmed Kandeel
"""
import json
FILE_NAME = "Tasks.json"
# =========================
# File Handling Functions
# =========================
def save_tasks(tasks):
"""Save tasks list to JSON file"""
with open(FILE_NAME, "w") as file:
json.dump(tasks, file, indent=4)
def load_tasks():
"""Load tasks from JSON file if exists"""
try:
with open(FILE_NAME, "r") as file:
return json.load(file)
except FileNotFoundError:
return []
# =========================
# Helper Functions
# =========================
def choose_task(tasks, message):
"""
Display tasks and allow user to choose one
Returns index of selected task or None
"""
for i, task in enumerate(tasks, 1):
print(f"{i}- {task['task']}")
try:
choice = int(input(message))
if 1 <= choice <= len(tasks):
return choice - 1
except ValueError:
pass
print("Invalid choice!")
return None
# =========================
# Core Application Functions
# =========================
def add_task(tasks):
"""Add a new task"""
task_name = input("Enter task name: ").strip().title()
if not task_name:
print("Task cannot be empty!")
return
if task_name.isdigit():
print("Task name must be text!")
return
add_note = input("Do you want to add a note? (y/n): ").lower()
task_data = {
"task": task_name,
"completed": False
}
if add_note in ("y", "yes"):
note = input("Enter note: ").strip().capitalize()
task_data["note"] = note
tasks.append(task_data)
save_tasks(tasks)
print("Task added successfully ✅")
def view_tasks(tasks):
"""Display all tasks"""
if not tasks:
print("No tasks to display.")
return
for i, task in enumerate(tasks, 1):
status = "✔️" if task["completed"] else "❌"
print(f"{i}. {task['task']} {status}")
if "note" in task:
print(f" 📝 Note: {task['note']}")
print("-" * 30)
def mark_task(tasks):
"""Mark a task as completed"""
# NOTE:
# Modifying incomplete_tasks updates the original task objects
# because they reference the same dictionaries
incomplete_tasks = [task for task in tasks if not task["completed"]]
if not incomplete_tasks:
print("No incomplete tasks.")
return
index = choose_task(incomplete_tasks, "Choose completed task number: ")
if index is None:
return
incomplete_tasks[index]["completed"] = True
save_tasks(tasks)
print("Task marked as completed 🎉")
def rename_task(tasks):
"""Rename an existing task"""
if not tasks:
print("No tasks to rename.")
return
index = choose_task(tasks, "Choose task number to rename: ")
if index is None:
return
new_name = input("Enter new task name: ").strip().title()
if not new_name or new_name.isdigit():
print("Invalid task name!")
return
tasks[index]["task"] = new_name
save_tasks(tasks)
print("Task renamed successfully ✏️")
def remove_task(tasks):
"""Remove a task"""
if not tasks:
print("No tasks to remove.")
return
index = choose_task(tasks, "Choose task number to remove: ")
if index is None:
return
removed_task = tasks.pop(index)
save_tasks(tasks)
print(f"Task '{removed_task['task']}' removed successfully 🗑️")
# =========================
# Main Application Loop
# =========================
def main():
tasks = load_tasks()
print("Welcome to Task Manager 👋")
while True:
print("""
1- Add Task
2- Mark Task as Completed
3- View Tasks
4- Remove Task
5- Rename Task
6- Quit
""")
choice = input("Choose an option: ").strip()
if choice == "1":
add_task(tasks)
elif choice == "2":
mark_task(tasks)
elif choice == "3":
view_tasks(tasks)
elif choice == "4":
remove_task(tasks)
elif choice == "5":
rename_task(tasks)
elif choice == "6":
print("Goodbye 👋")
break
else:
print("Invalid choice, please try again!")
if __name__ == "__main__":
main()