Python - Build a basic To-Do List Project: Solutions and Explanations
To-Do List Application:
Build a basic to-do list application where users can add, edit, and delete tasks.
Input values:
User interacts with the application through commands to add, edit, or delete tasks.
Output value:
Updated the to-do list based on user actions.
Example:
Input values: 1. Add Task 2. Edit Task 3. Delete Task 4. Exit Select an option: 1 Enter task: Buy groceries Output value: Task added successfully.
Input values: 1. Add Task 2. Edit Task 3. Delete Task 4. Exit Select an option: 2 Enter task index to edit: 1 Enter new task: Buy weekly groceries Output value: Task edited successfully.
Input values: 1. Add Task 2. Edit Task 3. Delete Task 4. Exit Select an option: 3 Enter task index to delete: 1 Output value: Task deleted successfully.
Input values: 1. Add Task 2. Edit Task 3. Delete Task 4. Exit Select an option: 4
Here are two different solutions for a basic to-do list application in Python. This application will allow users to add, edit, and delete tasks using a command-line interface.
Solution 1: Basic Approach Using a While Loop and List Operations
Code:
Output:
1. Add Task 2. Edit Task 3. Delete Task 4. Exit Select an option: 1 Enter task: Buy daily groceries Task added successfully. 1. Add Task 2. Edit Task 3. Delete Task 4. Exit Select an option: 2 1. Buy daily groceries Enter task index to edit: 1 Enter new task: Buy weekly groceries Task edited successfully. 1. Add Task 2. Edit Task 3. Delete Task 4. Exit Select an option: 3 1. Buy weekly groceries Enter task index to delete: 1 Task deleted successfully. 1. Add Task 2. Edit Task 3. Delete Task 4. Exit Select an option: 4 Exiting the application. Goodbye!
Explanation:
- Uses a simple list ('tasks') to store tasks and a 'while' loop to provide a continuous menu-driven interface.
- Contains menu options for adding, editing, and deleting tasks.
- Utilizes basic list operations ('append', 'pop', and direct indexing) to manage tasks.
- Handles user input and errors with simple conditionals.
- This approach is straightforward but less organized as the code grows.
Solution 2: Using a Class to Encapsulate the To-Do List Functionality
Code:
Output:
1. Add Task 2. Edit Task 3. Delete Task 4. Exit Select an option: 1 Enter task: Buy daily groceries Task added successfully. 1. Add Task 2. Edit Task 3. Delete Task 4. Exit Select an option: 2 1. Buy daily groceries Enter task index to edit: Buy weekly groceries Please enter a valid number. 1. Add Task 2. Edit Task 3. Delete Task 4. Exit Select an option: 3 1. Buy daily groceries Enter task index to delete: 1 Task deleted successfully. 1. Add Task 2. Edit Task 3. Delete Task 4. Exit Select an option: 4 Exiting the application. Goodbye!
Explanation:
- Uses a 'ToDoList' class to encapsulate all functionality related to the to-do list, making the code modular and easy to maintain.
- The class has methods for adding ('add_task'), editing ('edit_task'), deleting ('delete_task'), and displaying tasks ('display_tasks').
- The 'run' method manages the application's main loop, calling other methods as needed.
- This approach is more organized and makes the code easier to extend and maintain by leveraging Object-Oriented Programming (OOP) principles.
Note:
Both solutions achieve the required functionality for a to-do list application, but Solution 2 provides a more modular, organized, and scalable structure by using OOP principles.