Welcome to our mini-project! In this chapter, we're going to consolidate everything we've learned into a practical, real-world application. We'll be building a simple command-line To-Do List application. This project will give you a chance to practice using variables, lists, loops, conditional statements, and functions in a meaningful way. By the end of this project, you'll have a working application and a much deeper understanding of how these Python concepts come together.
Our To-Do List application will allow users to perform the following actions:
- Add a new task: Users can input a description for a new task, and it will be added to their list.
- View all tasks: The application will display all current tasks, along with their status (e.g., whether they are completed or not).
- Mark a task as complete: Users can select a task from the list and mark it as done.
- Remove a task: Users can remove a task from the list entirely.
- Exit the application: Users can quit the program when they are finished.
We'll design this application to be interactive and menu-driven. The user will be presented with a menu of options, and they can choose the action they want to perform by entering a corresponding number or character. This makes the application user-friendly and guides them through its features.
graph TD
A[Start Application] --> B{Display Menu}
B --> C{User Input}
C -- 'Add Task' --> D[Add New Task Function]
C -- 'View Tasks' --> E[View Tasks Function]
C -- 'Mark Complete' --> F[Mark Task Complete Function]
C -- 'Remove Task' --> G[Remove Task Function]
C -- 'Exit' --> H[End Application]
D --> B
E --> B
F --> B
G --> B
The core data structure we'll use to store our to-do items will be a Python list. Each item in the list will likely be a dictionary or a custom object (though for simplicity, we'll start with dictionaries) that holds information about the task, such as its description and its completion status.
todo_list = []
# Example task structure:
# {"description": "Buy groceries", "completed": False}We'll also be using functions to encapsulate each of the core functionalities (adding, viewing, marking complete, removing). This promotes modularity, making our code cleaner, easier to read, and simpler to debug. We'll also implement a main loop that continuously displays the menu and processes user input until the user chooses to exit.