- Blog/
How programming can help reduce mental load 🦾
·325 words·2 mins
Welcome to aichat 0.17.0
Type “.help” for additional help.
alm> I need help! My mental load is too much. Write some python code to help me get organized! Sure, here is a simple Python script that can help you manage your tasks. It allows you to add, view, and remove tasks from a to-do list.
import json
import os
FILENAME = 'todo_list.json'
def load_tasks():
if os.path.exists(FILENAME):
with open(FILENAME, 'r') as file:
return json.load(file)
return []
def save_tasks(tasks):
with open(FILENAME, 'w') as file:
json.dump(tasks, file, indent=4)
def add_task(task):
tasks = load_tasks()
tasks.append({"task": task, "completed": False})
save_tasks(tasks)
print(f'Task "{task}" added!')
def view_tasks():
tasks = load_tasks()
if not tasks:
print("No tasks available.")
else:
for i, item in enumerate(tasks):
status = "Done" if item["completed"] else "Not Done"
print(f"{i + 1}. {item['task']} - {status}")
def remove_task(task_number):
tasks = load_tasks()
if 0 < task_number <= len(tasks):
removed = tasks.pop(task_number - 1)
save_tasks(tasks)
print(f'Task "{removed["task"]}" removed!')
else:
print("Invalid task number.")
def mark_task_as_done(task_number):
tasks = load_tasks()
if 0 < task_number <= len(tasks):
tasks[task_number - 1]["completed"] = True
save_tasks(tasks)
print(f'Task "{tasks[task_number - 1]["task"]}" marked as done!')
else:
print("Invalid task number.")
def main():
while True:
print("\n1. Add Task\n2. View Tasks\n3. Remove Task\n4. Mark Task as Done\n5. Exit")
choice = input("Enter your choice: ")
if choice == '1':
task = input("Enter the task: ")
add_task(task)
elif choice == '2':
view_tasks()
elif choice == '3':
task_number = int(input("Enter task number to remove: "))
remove_task(task_number)
elif choice == '4':
task_number = int(input("Enter task number to mark as done: "))
mark_task_as_done(task_number)
elif choice == '5':
break
else:
print("Invalid choice! Please try again.")
if __name__ == "__main__":
main()
This script uses a simple JSON file to store your tasks. You can:
- Add a new task.
- View all tasks.
- Remove a task by its number.
- Mark a task as done.
- Exit the program.
To run the script, simply execute the Python file in your terminal or command prompt. This should give you a basic task manager to help you get organized.