From 4945cc678072933b760f2a809e0631e35da7111c Mon Sep 17 00:00:00 2001 From: Ocean Kumar <67411162+oceankumar@users.noreply.github.com> Date: Tue, 7 Oct 2025 19:24:12 +0530 Subject: [PATCH] Create todo.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Problem The repository lacks a simple, beginner-friendly Python example suitable for Hacktoberfest contributions. # Solution Add a minimal To-Do CLI app (`todo.py`) that saves tasks to `tasks.json` and a README with usage instructions. ## Changes Proposed - Add `todo.py` (no external dependencies) - Add `README-HACKTOBERFEST.md` with setup and commands - Basic validation for empty titles and invalid indices ## Other Notes - Tested locally with Python 3.7+ - Safe, self-contained addition that won’t affect existing code --- todo.py | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 todo.py diff --git a/todo.py b/todo.py new file mode 100644 index 0000000000..3acb2fc413 --- /dev/null +++ b/todo.py @@ -0,0 +1,104 @@ +import json +import os + +DATA_FILE = "tasks.json" + +def load_tasks(): + if not os.path.exists(DATA_FILE): + return [] + try: + with open(DATA_FILE, "r", encoding="utf-8") as f: + return json.load(f) + except (json.JSONDecodeError, OSError): + return [] + +def save_tasks(tasks): + with open(DATA_FILE, "w", encoding="utf-8") as f: + json.dump(tasks, f, ensure_ascii=False, indent=2) + +def list_tasks(tasks): + if not tasks: + print("No tasks.") + return + for i, t in enumerate(tasks, 1): + status = "✔" if t["done"] else "✗" + print(f"{i}. [{status}] {t['title']}") + +def add_task(tasks, title): + title = title.strip() + if not title: + print("Title cannot be empty.") + return + tasks.append({"title": title, "done": False}) + save_tasks(tasks) + print("Added.") + +def toggle_task(tasks, index): + if index < 1 or index > len(tasks): + print("Invalid task number.") + return + tasks[index - 1]["done"] = not tasks[index - 1]["done"] + save_tasks(tasks) + print("Updated.") + +def delete_task(tasks, index): + if index < 1 or index > len(tasks): + print("Invalid task number.") + return + tasks.pop(index - 1) + save_tasks(tasks) + print("Deleted.") + +def help_menu(): + print("Commands:") + print(" list Show tasks") + print(" add