To-Do List Application
In this mini project, we’ll build a simple but useful command-line To-Do List application. You’ll learn how to handle lists, functions, loops, and even file saving.
We’ll start simple and then add features step by step. This project will help you understand how to break a big task into smaller parts and use Copilot effectively to guide you through each piece.
Step 1: Create the basic structure
The first step is to build the core logic for adding, removing, and viewing tasks.
Prompt for Copilot:
1
# Create a basic to-do list application where users can add, remove, and view tasks using a list
Example suggestion from Copilot:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
tasks = [] def add_task(task): tasks.append(task) print(f"Task '{task}' added.") def remove_task(task): if task in tasks: tasks.remove(task) print(f"Task '{task}' removed.") else: print("Task not found.") def view_tasks(): if not tasks: print("No tasks available.") else: print("Your tasks:") for i, task in enumerate(tasks, start=1): print(f"{i}. {task}")
Explanation:
- We have a list called
tasks
to store all our tasks. add_task()
appends a new task to the list.remove_task()
checks if the task exists and removes it if found.view_tasks()
prints out all tasks in a numbered list so they’re easy to read.
Step 2: Add a menu loop
We want the user to be able to perform multiple actions without restarting the program.
Prompt for Copilot:
1
# Create a loop to show options to the user: add, remove, view, and quit
Example suggestion from Copilot:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
while True: print("\nOptions: add, remove, view, quit") choice = input("What would you like to do? ").strip().lower() if choice == "add": task = input("Enter a task: ") add_task(task) elif choice == "remove": task = input("Enter the task to remove: ") remove_task(task) elif choice == "view": view_tasks() elif choice == "quit": print("Goodbye!") break else: print("Invalid choice. Please try again.")
Explanation:
- The program runs in an infinite
while True
loop. - The user is asked to choose an action: add, remove, view, or quit.
- Depending on the choice, the appropriate function is called.
break
stops the loop when the user chooses to quit.
Step 3: Add file saving
Now let’s make the app more useful by allowing it to save tasks to a file so they aren’t lost when we close the program.
Prompt for Copilot:
1
# Update the to-do list application to save tasks to a file and load them when starting
Example suggestion from Copilot:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
import os FILENAME = "tasks.txt" def load_tasks(): if os.path.exists(FILENAME): with open(FILENAME, "r") as file: for line in file: tasks.append(line.strip()) def save_tasks(): with open(FILENAME, "w") as file: for task in tasks: file.write(task + "\n") # Load tasks at the start load_tasks() while True: print("\nOptions: add, remove, view, quit") choice = input("What would you like to do? ").strip().lower() if choice == "add": task = input("Enter a task: ") add_task(task) save_tasks() elif choice == "remove": task = input("Enter the task to remove: ") remove_task(task) save_tasks() elif choice == "view": view_tasks() elif choice == "quit": save_tasks() print("Tasks saved. Goodbye!") break else: print("Invalid choice. Please try again.")
Explanation:
- We create a constant
FILENAME
to specify the file where tasks will be saved. load_tasks()
reads tasks from the file when the program starts and appends them to our list.save_tasks()
writes all current tasks to the file each time the list changes.- We call
save_tasks()
after adding or removing tasks and right before quitting to ensure changes are always saved.
Step 4: Extend your project
Once your basic to-do list app is working, you can try these optional challenges to make it even better:
- Add a feature to mark tasks as completed and display them separately.
- Save the completion status to the file so that it persists.
- Add priority levels (e.g., High, Medium, Low) to tasks.
- Sort tasks alphabetically or by priority.
- Add timestamps when tasks are created or completed.
Prompt to try:
1
# Add a feature to mark tasks as completed and show completed tasks separately
Summary
In this mini project, you learned to:
- Use lists to store data dynamically.
- Write and call functions to organize logic clearly.
- Use loops and conditionals to build interactive command-line applications.
- Read and write files to save user data.
- Use Copilot prompts to quickly scaffold your code and then refine it.