Skip to content

Commit 8909f58

Browse files
authored
Add files via upload
1 parent 9419fe9 commit 8909f58

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

To_Do_list.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import json
2+
3+
TASKS_FILE = "tasks.json"
4+
tasks = []
5+
6+
def load_tasks():
7+
global tasks
8+
try:
9+
with open(TASKS_FILE, "r") as file:
10+
tasks = json.load(file)
11+
print("Tasks loaded successfully.")
12+
except (FileNotFoundError, json.JSONDecodeError):
13+
print("No existing task file found. Starting with an empty list.")
14+
tasks = []
15+
16+
def save_tasks():
17+
with open(TASKS_FILE, "w") as file:
18+
json.dump(tasks, file, indent=4)
19+
print("Tasks saved.")
20+
21+
# The rest of the functions (show_tasks, add_task, complete_task, delete_task, save_report_to_file)
22+
# remain the same as in your original code. The changes are primarily in the main function.
23+
# Please copy your original functions here.
24+
# ... (your original functions) ...
25+
def show_tasks():
26+
if not tasks:
27+
print("No tasks available.")
28+
else:
29+
print("\nYour Tasks:")
30+
for i, task in enumerate(tasks, 1):
31+
status = "✓" if task['completed'] else "✗"
32+
print(f"{i}. [{status}] {task['title']}")
33+
34+
def add_task():
35+
title = input("Enter task title: ").strip()
36+
if title:
37+
tasks.append({'title': title, 'completed': False})
38+
print(f"Task '{title}' added.")
39+
else:
40+
print("Task title cannot be empty.")
41+
42+
def complete_task():
43+
show_tasks()
44+
if tasks:
45+
try:
46+
task_num = int(input("Enter task number to mark as completed: "))
47+
if 1 <= task_num <= len(tasks):
48+
tasks[task_num - 1]['completed'] = True
49+
print(f"Task '{tasks[task_num - 1]['title']}' marked as completed.")
50+
else:
51+
print("Invalid task number.")
52+
except ValueError:
53+
print("Please enter a valid number.")
54+
55+
def delete_task():
56+
show_tasks()
57+
if tasks:
58+
try:
59+
task_num = int(input("Enter task number to delete: "))
60+
if 1 <= task_num <= len(tasks):
61+
removed = tasks.pop(task_num - 1)
62+
print(f"Task '{removed['title']}' deleted.")
63+
else:
64+
print("Invalid task number.")
65+
except ValueError:
66+
print("Please enter a valid number.")
67+
68+
def save_report_to_file():
69+
if not tasks:
70+
print("No tasks to save.")
71+
return
72+
73+
completed_count = sum(task['completed'] for task in tasks)
74+
remaining_count = len(tasks) - completed_count
75+
76+
try:
77+
with open("task_report.txt", "w", encoding="utf-8") as file:
78+
file.write("Task Report\n")
79+
file.write("===========\n\n")
80+
for i, task in enumerate(tasks, 1):
81+
status = "Completed" if task['completed'] else "Pending"
82+
file.write(f"{i}. [{status}] {task['title']}\n")
83+
84+
file.write("\n")
85+
file.write(f"Total tasks: {len(tasks)}\n")
86+
file.write(f"Completed tasks: {completed_count}\n")
87+
file.write(f"Remaining tasks: {remaining_count}\n")
88+
89+
print("Task report saved to 'task_report.txt'.")
90+
except Exception as e:
91+
print(f"Error saving file: {e}")
92+
93+
def main():
94+
load_tasks() # Load tasks at the start
95+
96+
while True:
97+
print("\nTask Manager")
98+
print("1. Show Tasks")
99+
print("2. Add Task")
100+
print("3. Complete Task")
101+
print("4. Delete Task")
102+
print("5. Save Task Report to File")
103+
print("6. Exit")
104+
105+
choice = input("Choose an option: ").strip()
106+
107+
if choice == '1':
108+
show_tasks()
109+
elif choice == '2':
110+
add_task()
111+
save_tasks() # Save after adding
112+
elif choice == '3':
113+
complete_task()
114+
save_tasks() # Save after completing
115+
elif choice == '4':
116+
delete_task()
117+
save_tasks() # Save after deleting
118+
elif choice == '5':
119+
save_report_to_file()
120+
elif choice == '6':
121+
print("Goodbye!")
122+
break
123+
else:
124+
print("Invalid choice. Please select a valid option.")
125+
126+
if __name__ == "__main__":
127+
main()

0 commit comments

Comments
 (0)