Spaces:
No application file
No application file
Upload nocturnal (1).py
Browse files- nocturnal (1).py +105 -0
nocturnal (1).py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
FILE_NAME = "tasks.json"
|
| 5 |
+
tasks = []
|
| 6 |
+
|
| 7 |
+
def load_tasks():
|
| 8 |
+
"""Attempts to load tasks from the JSON file if it exists."""
|
| 9 |
+
global tasks
|
| 10 |
+
if os.path.exists(FILE_NAME):
|
| 11 |
+
try:
|
| 12 |
+
with open(FILE_NAME, 'r') as file:
|
| 13 |
+
tasks = json.load(file)
|
| 14 |
+
print(f"β
Tasks successfully loaded from {FILE_NAME}.")
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"β Error loading tasks: {e}")
|
| 17 |
+
tasks = []
|
| 18 |
+
else:
|
| 19 |
+
print(f"π No previous task file ({FILE_NAME}) found. Starting with an empty list.")
|
| 20 |
+
|
| 21 |
+
def save_tasks():
|
| 22 |
+
"""Saves the current list of tasks to the JSON file."""
|
| 23 |
+
try:
|
| 24 |
+
with open(FILE_NAME, 'w') as file:
|
| 25 |
+
json.dump(tasks, file, indent=4)
|
| 26 |
+
print(f"β
Tasks successfully saved to {FILE_NAME}.")
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"β Error saving tasks: {e}")
|
| 29 |
+
|
| 30 |
+
def add_task(task_description):
|
| 31 |
+
"""Adds a new task to the list with a 'completed' status."""
|
| 32 |
+
tasks.append({"task": task_description, "completed": False})
|
| 33 |
+
print(f"β¨ Task added: '{task_description}'")
|
| 34 |
+
|
| 35 |
+
def view_tasks():
|
| 36 |
+
"""Displays all tasks with their index and status."""
|
| 37 |
+
if not tasks:
|
| 38 |
+
print("
|
| 39 |
+
π Your To-Do List is empty. Time to add a task!")
|
| 40 |
+
return
|
| 41 |
+
print("
|
| 42 |
+
--- Current To-Do List ---")
|
| 43 |
+
for index, task in enumerate(tasks, start=1):
|
| 44 |
+
status = "β
" if task["completed"] else "β"
|
| 45 |
+
print(f"{index}. [{status}] {task['task']}")
|
| 46 |
+
print("--------------------------")
|
| 47 |
+
|
| 48 |
+
def delete_task(task_number):
|
| 49 |
+
"""Deletes a task based on its number (index + 1)."""
|
| 50 |
+
try:
|
| 51 |
+
task_index = int(task_number) - 1
|
| 52 |
+
if 0 <= task_index < len(tasks):
|
| 53 |
+
deleted_task = tasks.pop(task_index)
|
| 54 |
+
print(f"ποΈ Deleted task: '{deleted_task['task']}'")
|
| 55 |
+
else:
|
| 56 |
+
print("β Invalid task number. Please try again.")
|
| 57 |
+
except ValueError:
|
| 58 |
+
print("β Invalid input. Please enter a number.")
|
| 59 |
+
|
| 60 |
+
def mark_completed(task_number):
|
| 61 |
+
"""Marks a task as completed based on its number (index + 1)."""
|
| 62 |
+
try:
|
| 63 |
+
task_index = int(task_number) - 1
|
| 64 |
+
if 0 <= task_index < len(tasks):
|
| 65 |
+
tasks[task_index]["completed"] = True
|
| 66 |
+
print(f"π Marked as completed: '{tasks[task_index]['task']}'")
|
| 67 |
+
else:
|
| 68 |
+
print("β Invalid task number. Please try again.")
|
| 69 |
+
except ValueError:
|
| 70 |
+
print("β Invalid input. Please enter a number.")
|
| 71 |
+
|
| 72 |
+
def main_menu():
|
| 73 |
+
"""Displays the main menu and handles user choices."""
|
| 74 |
+
load_tasks()
|
| 75 |
+
while True:
|
| 76 |
+
view_tasks()
|
| 77 |
+
print("
|
| 78 |
+
--- COMMANDS ---")
|
| 79 |
+
print("1: Add a new task")
|
| 80 |
+
print("2: Delete a task (by number)")
|
| 81 |
+
print("3: Mark a task as completed (by number)")
|
| 82 |
+
print("4: Save & Exit")
|
| 83 |
+
print("----------------")
|
| 84 |
+
choice = input("Select a command (1-4): ")
|
| 85 |
+
if choice == '1':
|
| 86 |
+
task_desc = input("Enter the task description: ")
|
| 87 |
+
if task_desc:
|
| 88 |
+
add_task(task_desc)
|
| 89 |
+
else:
|
| 90 |
+
print("Task description cannot be empty.")
|
| 91 |
+
elif choice == '2':
|
| 92 |
+
task_num = input("Enter the number of the task to delete: ")
|
| 93 |
+
delete_task(task_num)
|
| 94 |
+
elif choice == '3':
|
| 95 |
+
task_num = input("Enter the number of the task to mark as completed: ")
|
| 96 |
+
mark_completed(task_num)
|
| 97 |
+
elif choice == '4':
|
| 98 |
+
save_tasks()
|
| 99 |
+
print("π System shutting down. Goodbye!")
|
| 100 |
+
break
|
| 101 |
+
else:
|
| 102 |
+
print("β Invalid choice. Please select a number from 1 to 4.")
|
| 103 |
+
|
| 104 |
+
if __name__ == "__main__":
|
| 105 |
+
main_menu()
|