r/PythonLearning • • Apr 27 '26

Showcase the task thing’s easy 😅

Post image

i wrote it but the save/load part is ai, rest is all me. what y'all think?

63 Upvotes

21 comments sorted by

View all comments

2

u/joetato_of_syracuse Apr 28 '26

m o n o s p a c e

2

u/l__lj Apr 28 '26

import json

def show_menu(): print("""

  1. show menu

  2. show all tasks

  3. add task

  4. delete tasks

  5. toggle tasks status

  6. edit tasks

  7. save tasks

  8. load tasks

  9. exit """) def show_all_tasks(tasks): if not tasks: print("not tasks.") else: for i,task in enumerate(tasks , start=1): status = "✓" if task["completed"] else "×" print(f"{i}.{task['task']} {status}") print(("-"*30)) def add_tasks(tasks): task_text = input("Enter new task : \n") new_task = {'task': task_text, 'completed':False} tasks.append(new_task) print("Task added successfully")

def delete_tasks(tasks): try: index = int(input("task number u wanna delete:\n")) if 0 < index <= len(tasks): tasks.pop(index - 1 ) print("Task deleted successfully!")

    else:
        print("Invalid task number.")
except ValueError:
    print("Please enter a valid number")

def toggle_status(tasks): try: num = int(input("task number u wanna edit:\n")) index = num - 1 if 0<= index < len(tasks): tasks[index]['completed'] = not tasks[index]['completed'] status = "✓" if tasks[index]["completed"] else "×" print(f"Task #{num} status changed to: {status}") except ValueError: print("Please enter a valid number")

def edit_tasks(tasks): try: num = int(input("task number u wanna edit:\n")) index = num - 1 if 0 <= index < len(tasks): new_task = input("enter new task:\n") tasks[index]['task'] = new_task print("Task updated successfully!") else: print("Invalid task number") except ValueError: print("Please enter a valid number")

def save_tasks(tasks): try: with open("tasks.json" , "w" , encoding = "utf-8") as file: json.dump(tasks, file, ensure_ascii= False, indent=4) print("✅ Tasks saved successfully!") except Exception as e: print(f"❌ Error saving tasks: {e}")

def load_tasks(): try: with open("tasks.json", "r", encoding = "utf-8") as file: tasks = json.load(file) print("✅ Tasks loaded successfully!") return tasks except FileNotFoundError: print("⚠️ No saved tasks found. Starting with empty list.") return [] except Exception as e: print(f"❌ Error loading tasks: {e}") return [] tasks = load_tasks()

while True: try: choice = int(input("Enter number: \n")) except ValueError: print("not a number!") print("Please pick a number from the list!") continue if choice == 0: print("Goodbye") break elif choice == 1: show_menu() elif choice == 2: show_all_tasks(tasks) elif choice == 3: add_tasks(tasks) elif choice == 4: delete_tasks(tasks) elif choice == 5: toggle_status(tasks) elif choice == 6: edit_tasks(tasks) elif choice == 7: save_tasks(tasks) elif choice == 8 : load_tasks() else: print("Please pick a number from the list!")