Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
266 lines (211 loc) · 7.02 KB
/
Copy pathmain.py
File metadata and controls
266 lines (211 loc) · 7.02 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"""
Basic To-Do List - Python Project 05
====================================
A simple command-line to-do list manager with file persistence.
🎯 LEARNING OBJECTIVES:
- Work with lists (add, remove, display)
- Store data in text files (file I/O)
- Handle file operations (read, write, append)
- Create a persistent data application
- Implement CRUD operations (Create, Read, Update, Delete)
📚 CONCEPTS COVERED:
- File handling (open, read, write, close)
- List operations (append, remove, enumerate)
- String methods (strip, split, join)
- Exception handling (FileNotFoundError)
- Data persistence across program runs
"""
import os
# File to store tasks
TASKS_FILE = "tasks.txt"
def load_tasks():
"""
Load tasks from file into a list.
Returns:
list: List of task strings
LEARNING: Reading from files, handling missing files
"""
if not os.path.exists(TASKS_FILE):
return [] # Return empty list if file doesn't exist
try:
with open(TASKS_FILE, 'r') as file:
# Read lines and remove newline characters
tasks = [line.strip() for line in file.readlines()]
return [task for task in tasks if task] # Remove empty lines
except Exception as e:
print(f"⚠️ Error loading tasks: {e}")
return []
def save_tasks(tasks):
"""
Save tasks list to file.
Args:
tasks (list): List of task strings
LEARNING: Writing to files, overwriting existing content
"""
try:
with open(TASKS_FILE, 'w') as file:
for task in tasks:
file.write(task + '\n')
return True
except Exception as e:
print(f"⚠️ Error saving tasks: {e}")
return False
def display_tasks(tasks):
"""
Display all tasks with numbers.
LEARNING: Using enumerate() to get index and value
"""
if not tasks:
print("\n📭 No tasks yet! Add some to get started.")
return
print("\n" + "="*60)
print("📝 YOUR TO-DO LIST")
print("="*60)
for index, task in enumerate(tasks, start=1):
print(f"{index}. {task}")
print("="*60)
print(f"Total tasks: {len(tasks)}")
def add_task(tasks):
"""
Add a new task to the list.
LEARNING: Getting user input and appending to lists
"""
task = input("\nEnter new task: ").strip()
if not task:
print("❌ Task cannot be empty!")
return tasks
tasks.append(task)
print(f"✅ Added: '{task}'")
return tasks
def remove_task(tasks):
"""
Remove a task by its number.
LEARNING: Removing items from lists by index
"""
if not tasks:
print("\n📭 No tasks to remove!")
return tasks
display_tasks(tasks)
try:
task_num = int(input("\nEnter task number to remove: "))
if 1 <= task_num <= len(tasks):
removed_task = tasks.pop(task_num - 1) # Convert to 0-based index
print(f"✅ Removed: '{removed_task}'")
else:
print(f"❌ Invalid task number! Choose 1-{len(tasks)}")
except ValueError:
print("❌ Please enter a valid number!")
return tasks
def clear_all_tasks(tasks):
"""
Remove all tasks after confirmation.
LEARNING: User confirmation for destructive operations
"""
if not tasks:
print("\n📭 No tasks to clear!")
return tasks
confirm = input(f"\n⚠️ Are you sure you want to clear all {len(tasks)} tasks? (y/n): ").lower()
if confirm == 'y':
tasks.clear()
print("✅ All tasks cleared!")
else:
print("❌ Cancelled")
return tasks
def mark_task_done(tasks):
"""
Mark a task as done by adding [DONE] prefix.
LEARNING: Modifying list items
"""
if not tasks:
print("\n📭 No tasks to mark!")
return tasks
display_tasks(tasks)
try:
task_num = int(input("\nEnter task number to mark as done: "))
if 1 <= task_num <= len(tasks):
# Check if already marked
if tasks[task_num - 1].startswith("✓ "):
print("ℹ️ Task already marked as done!")
else:
tasks[task_num - 1] = "✓ " + tasks[task_num - 1]
print("✅ Task marked as done!")
else:
print(f"❌ Invalid task number! Choose 1-{len(tasks)}")
except ValueError:
print("❌ Please enter a valid number!")
return tasks
def display_menu():
"""
Display main menu options.
"""
print("\n" + "="*60)
print("📝 TO-DO LIST MENU")
print("="*60)
print("1. View all tasks")
print("2. Add new task")
print("3. Remove task")
print("4. Mark task as done")
print("5. Clear all tasks")
print("6. Save and exit")
print("="*60)
def main():
"""
Main program loop.
LEARNING:
- Program lifecycle (load → operate → save)
- Menu-driven applications
- Data persistence
"""
print("\n🎯 Welcome to Your To-Do List Manager!")
print("📂 Loading tasks...")
# Load existing tasks from file
tasks = load_tasks()
print(f"✅ Loaded {len(tasks)} task(s)")
# Main loop
while True:
display_menu()
choice = input("\nEnter your choice (1-6): ").strip()
if choice == '1':
# View tasks
display_tasks(tasks)
elif choice == '2':
# Add task
tasks = add_task(tasks)
elif choice == '3':
# Remove task
tasks = remove_task(tasks)
elif choice == '4':
# Mark as done
tasks = mark_task_done(tasks)
elif choice == '5':
# Clear all
tasks = clear_all_tasks(tasks)
elif choice == '6':
# Save and exit
print("\n💾 Saving tasks...")
if save_tasks(tasks):
print("✅ Tasks saved successfully!")
print(f"📁 Saved to: {TASKS_FILE}")
print("\n👋 Goodbye! Your tasks will be here next time.")
break
else:
print("⚠️ Error saving tasks!")
retry = input("Try again? (y/n): ").lower()
if retry != 'y':
break
else:
print("❌ Invalid choice! Please select 1-6.")
if __name__ == "__main__":
main()
# ============================================================================
# FILE I/O CONCEPTS EXPLAINED:
# ============================================================================
# 'r' mode: Read (file must exist)
# 'w' mode: Write (creates new or overwrites existing)
# 'a' mode: Append (adds to end of file)
# 'with' statement: Automatically closes file (best practice)
#
# file.read() - reads entire file as string
# file.readlines() - reads file as list of lines
# file.write(text) - writes text to file
# file.writelines(list) - writes list of strings to file