-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_task_tracker.py
More file actions
98 lines (80 loc) · 2.48 KB
/
simple_task_tracker.py
File metadata and controls
98 lines (80 loc) · 2.48 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
import json
import os
class TaskManager:
def __init__(self, filename="tasks.json"):
self.filename = filename
self.tasks = []
self.load_tasks()
# Load tasks from JSON file
def load_tasks(self):
if os.path.exists(self.filename):
try:
with open(self.filename, "r") as file:
self.tasks = json.load(file)
except json.JSONDecodeError:
self.tasks = []
else:
self.tasks = []
# Save tasks to JSON file
def save_tasks(self):
with open(self.filename, "w") as file:
json.dump(self.tasks, file, indent=4)
# Add Task
def add_task(self):
title = input("Enter Title: ")
description = input("Enter Description: ")
task = {
"title": title,
"description": description
}
self.tasks.append(task)
self.save_tasks()
print("Task added successfully!\n")
# View Tasks
def view_tasks(self):
if not self.tasks:
print("No tasks available.\n")
return
print("\nYour Tasks:")
for index, task in enumerate(self.tasks, start=1):
print(f"{index}. {task['title']} - {task['description']}")
print()
# Delete Task
def delete_task(self):
if not self.tasks:
print("No tasks to delete.\n")
return
self.view_tasks()
try:
choice = int(input("Enter task number to delete: "))
if 1 <= choice <= len(self.tasks):
removed = self.tasks.pop(choice - 1)
self.save_tasks()
print(f"Task '{removed['title']}' deleted successfully!\n")
else:
print("Invalid task number.\n")
except ValueError:
print("Please enter a valid number.\n")
# Main Program
def main():
manager = TaskManager()
while True:
print("===== Task Tracker =====")
print("1. Add Task")
print("2. View Tasks")
print("3. Delete Task")
print("4. Exit")
choice = input("Enter choice: ")
if choice == "1":
manager.add_task()
elif choice == "2":
manager.view_tasks()
elif choice == "3":
manager.delete_task()
elif choice == "4":
print("Exiting... Goodbye!")
break
else:
print("Invalid choice. Try again.\n")
if __name__ == "__main__":
main()