-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTo_Do_list.py
More file actions
127 lines (110 loc) · 4.07 KB
/
To_Do_list.py
File metadata and controls
127 lines (110 loc) · 4.07 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
import json
TASKS_FILE = "tasks.json"
tasks = []
def load_tasks():
global tasks
try:
with open(TASKS_FILE, "r") as file:
tasks = json.load(file)
print("Tasks loaded successfully.")
except (FileNotFoundError, json.JSONDecodeError):
print("No existing task file found. Starting with an empty list.")
tasks = []
def save_tasks():
with open(TASKS_FILE, "w") as file:
json.dump(tasks, file, indent=4)
print("Tasks saved.")
# The rest of the functions (show_tasks, add_task, complete_task, delete_task, save_report_to_file)
# remain the same as in your original code. The changes are primarily in the main function.
# Please copy your original functions here.
# ... (your original functions) ...
def show_tasks():
if not tasks:
print("No tasks available.")
else:
print("\nYour Tasks:")
for i, task in enumerate(tasks, 1):
status = "✓" if task['completed'] else "✗"
print(f"{i}. [{status}] {task['title']}")
def add_task():
title = input("Enter task title: ").strip()
if title:
tasks.append({'title': title, 'completed': False})
print(f"Task '{title}' added.")
else:
print("Task title cannot be empty.")
def complete_task():
show_tasks()
if tasks:
try:
task_num = int(input("Enter task number to mark as completed: "))
if 1 <= task_num <= len(tasks):
tasks[task_num - 1]['completed'] = True
print(f"Task '{tasks[task_num - 1]['title']}' marked as completed.")
else:
print("Invalid task number.")
except ValueError:
print("Please enter a valid number.")
def delete_task():
show_tasks()
if tasks:
try:
task_num = int(input("Enter task number to delete: "))
if 1 <= task_num <= len(tasks):
removed = tasks.pop(task_num - 1)
print(f"Task '{removed['title']}' deleted.")
else:
print("Invalid task number.")
except ValueError:
print("Please enter a valid number.")
def save_report_to_file():
if not tasks:
print("No tasks to save.")
return
completed_count = sum(task['completed'] for task in tasks)
remaining_count = len(tasks) - completed_count
try:
with open("task_report.txt", "w", encoding="utf-8") as file:
file.write("Task Report\n")
file.write("===========\n\n")
for i, task in enumerate(tasks, 1):
status = "Completed" if task['completed'] else "Pending"
file.write(f"{i}. [{status}] {task['title']}\n")
file.write("\n")
file.write(f"Total tasks: {len(tasks)}\n")
file.write(f"Completed tasks: {completed_count}\n")
file.write(f"Remaining tasks: {remaining_count}\n")
print("Task report saved to 'task_report.txt'.")
except Exception as e:
print(f"Error saving file: {e}")
def main():
load_tasks() # Load tasks at the start
while True:
print("\nTask Manager")
print("1. Show Tasks")
print("2. Add Task")
print("3. Complete Task")
print("4. Delete Task")
print("5. Save Task Report to File")
print("6. Exit")
choice = input("Choose an option: ").strip()
if choice == '1':
show_tasks()
elif choice == '2':
add_task()
save_tasks() # Save after adding
elif choice == '3':
complete_task()
save_tasks() # Save after completing
elif choice == '4':
delete_task()
save_tasks() # Save after deleting
elif choice == '5':
save_report_to_file()
elif choice == '6':
print("Goodbye!")
break
else:
print("Invalid choice. Please select a valid option.")
if __name__ == "__main__":
main()