Categories We Write About

Build a command-line to-do list app

Here’s a complete command-line To-Do List app in Python. It allows users to add, list, complete, and delete tasks using a simple terminal interface.

python
import os import json TODO_FILE = "todo_list.json" def load_tasks(): if not os.path.exists(TODO_FILE): return [] with open(TODO_FILE, "r") as file: return json.load(file) def save_tasks(tasks): with open(TODO_FILE, "w") as file: json.dump(tasks, file, indent=2) def list_tasks(tasks): if not tasks: print("No tasks in your to-do list.") else: for i, task in enumerate(tasks, 1): status = "✓" if task["completed"] else "✗" print(f"{i}. [{status}] {task['title']}") def add_task(tasks, title): tasks.append({"title": title, "completed": False}) save_tasks(tasks) print(f"Added task: {title}") def complete_task(tasks, index): if 0 <= index < len(tasks): tasks[index]["completed"] = True save_tasks(tasks) print(f"Marked task {index + 1} as completed.") else: print("Invalid task number.") def delete_task(tasks, index): if 0 <= index < len(tasks): removed = tasks.pop(index) save_tasks(tasks) print(f"Deleted task: {removed['title']}") else: print("Invalid task number.") def show_menu(): print("nTo-Do List App") print("1. List tasks") print("2. Add task") print("3. Complete task") print("4. Delete task") print("5. Exit") def main(): tasks = load_tasks() while True: show_menu() choice = input("Enter your choice (1-5): ").strip() if choice == "1": list_tasks(tasks) elif choice == "2": title = input("Enter task title: ").strip() if title: add_task(tasks, title) elif choice == "3": list_tasks(tasks) try: index = int(input("Enter task number to complete: ")) - 1 complete_task(tasks, index) except ValueError: print("Please enter a valid number.") elif choice == "4": list_tasks(tasks) try: index = int(input("Enter task number to delete: ")) - 1 delete_task(tasks, index) except ValueError: print("Please enter a valid number.") elif choice == "5": print("Goodbye!") break else: print("Invalid choice. Please select a valid option.") if __name__ == "__main__": main()

How it works:

  • Stores tasks in todo_list.json.

  • Tasks are represented as dictionaries with "title" and "completed" status.

  • Provides a simple menu to interact via the terminal.

You can run it using:

bash
python todo.py

Let me know if you’d like a version using a database, GUI, or a different language.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About