Spaces:
No application file
No application file
| import os | |
| import json | |
| FILE_NAME = "tasks.json" | |
| tasks = [] | |
| def load_tasks(): | |
| """Attempts to load tasks from the JSON file if it exists.""" | |
| global tasks | |
| if os.path.exists(FILE_NAME): | |
| try: | |
| with open(FILE_NAME, 'r') as file: | |
| tasks = json.load(file) | |
| print(f"β Tasks successfully loaded from {FILE_NAME}.") | |
| except Exception as e: | |
| print(f"β Error loading tasks: {e}") | |
| tasks = [] | |
| else: | |
| print(f"π No previous task file ({FILE_NAME}) found. Starting with an empty list.") | |
| def save_tasks(): | |
| """Saves the current list of tasks to the JSON file.""" | |
| try: | |
| with open(FILE_NAME, 'w') as file: | |
| json.dump(tasks, file, indent=4) | |
| print(f"β Tasks successfully saved to {FILE_NAME}.") | |
| except Exception as e: | |
| print(f"β Error saving tasks: {e}") | |
| def add_task(task_description): | |
| """Adds a new task to the list with a 'completed' status.""" | |
| tasks.append({"task": task_description, "completed": False}) | |
| print(f"β¨ Task added: '{task_description}'") | |
| def view_tasks(): | |
| """Displays all tasks with their index and status.""" | |
| if not tasks: | |
| print(" | |
| π Your To-Do List is empty. Time to add a task!") | |
| return | |
| print(" | |
| --- Current To-Do List ---") | |
| for index, task in enumerate(tasks, start=1): | |
| status = "β " if task["completed"] else "β" | |
| print(f"{index}. [{status}] {task['task']}") | |
| print("--------------------------") | |
| def delete_task(task_number): | |
| """Deletes a task based on its number (index + 1).""" | |
| try: | |
| task_index = int(task_number) - 1 | |
| if 0 <= task_index < len(tasks): | |
| deleted_task = tasks.pop(task_index) | |
| print(f"ποΈ Deleted task: '{deleted_task['task']}'") | |
| else: | |
| print("β Invalid task number. Please try again.") | |
| except ValueError: | |
| print("β Invalid input. Please enter a number.") | |
| def mark_completed(task_number): | |
| """Marks a task as completed based on its number (index + 1).""" | |
| try: | |
| task_index = int(task_number) - 1 | |
| if 0 <= task_index < len(tasks): | |
| tasks[task_index]["completed"] = True | |
| print(f"π Marked as completed: '{tasks[task_index]['task']}'") | |
| else: | |
| print("β Invalid task number. Please try again.") | |
| except ValueError: | |
| print("β Invalid input. Please enter a number.") | |
| def main_menu(): | |
| """Displays the main menu and handles user choices.""" | |
| load_tasks() | |
| while True: | |
| view_tasks() | |
| print(" | |
| --- COMMANDS ---") | |
| print("1: Add a new task") | |
| print("2: Delete a task (by number)") | |
| print("3: Mark a task as completed (by number)") | |
| print("4: Save & Exit") | |
| print("----------------") | |
| choice = input("Select a command (1-4): ") | |
| if choice == '1': | |
| task_desc = input("Enter the task description: ") | |
| if task_desc: | |
| add_task(task_desc) | |
| else: | |
| print("Task description cannot be empty.") | |
| elif choice == '2': | |
| task_num = input("Enter the number of the task to delete: ") | |
| delete_task(task_num) | |
| elif choice == '3': | |
| task_num = input("Enter the number of the task to mark as completed: ") | |
| mark_completed(task_num) | |
| elif choice == '4': | |
| save_tasks() | |
| print("π System shutting down. Goodbye!") | |
| break | |
| else: | |
| print("β Invalid choice. Please select a number from 1 to 4.") | |
| if __name__ == "__main__": | |
| main_menu() |