Introduction
A To-Do List application is a simple program that allows users to keep track of tasks they need to complete. It is a practical tool for organizing daily activities, setting goals, and managing time effectively.
The objective of this application is to create a basic command-line To-Do list where users can add tasks, view tasks, and mark tasks as completed. This is a beginner-friendly Python project that will introduce basic concepts such as lists, loops, and user input handling.
Code for the To-Do List Application
# Simple To-Do List Application in Python
# Function to display the menu options
def display_menu():
print("\nTo-Do List Menu:")
print("1. View To-Do List")
print("2. Add New Task")
print("3. Mark Task as Completed")
print("4. Exit")
# Function to display the to-do list
def view_tasks(tasks):
if not tasks:
print("\nNo tasks to show!")
else:
print("\nYour To-Do List:")
for i, task in enumerate(tasks, 1):
print(f"{i}. {task}")
# Function to add a task to the list
def add_task(tasks):
task = input("\nEnter the task to add: ")
tasks.append(task)
print(f"Task '{task}' added successfully!")
# Function to mark a task as completed
def mark_completed(tasks):
view_tasks(tasks)
if tasks:
task_number = int(input("\nEnter the task number to mark as completed: "))
if 0 < task_number <= len(tasks):
completed_task = tasks.pop(task_number - 1)
print(f"Task '{completed_task}' has been completed and removed from the list.")
else:
print("Invalid task number!")
else:
print("No tasks to mark as completed.")
# Main function to run the to-do list application
def todo_list_app():
tasks = []
while True:
display_menu()
choice = input("\nEnter your choice (1-4): ")
if choice == '1':
view_tasks(tasks)
elif choice == '2':
add_task(tasks)
elif choice == '3':
mark_completed(tasks)
elif choice == '4':
print("\nThank you for using the To-Do List App! Goodbye!")
break
else:
print("\nInvalid choice! Please enter a number between 1 and 4.")
# Run the application
if __name__ == "__main__":
todo_list_app()
Explanation of the Program Structure
The To-Do List application consists of several components:
- display_menu() function: This function displays the main menu with options to view the tasks, add a new task, mark a task as completed, or exit the program.
- view_tasks() function: Displays the current list of tasks. If the list is empty, it informs the user that there are no tasks.
- add_task() function: This function prompts the user to input a task and adds it to the list of tasks.
- mark_completed() function: Allows the user to mark a task as completed by entering the task number. It then removes the completed task from the list.
- todo_list_app() function: This is the main driver function of the application that runs in a loop until the user decides to exit. It calls the other functions based on the user’s input.
The program allows the user to interact with a simple text-based menu. The user can add tasks to the list, view the list, and mark tasks as completed. The program continues to run until the user opts to exit.
How to Run the Program
To run the To-Do List application:
- Ensure you have Python installed on your system. You can download Python from here.
- Create a new Python file (e.g.,
todo_list.py
) and paste the code above into the file. - Open a terminal or command prompt, navigate to the folder where the Python file is located, and run the program by typing the following command:
python todo_list.py
- The program will display a menu, and you can interact with it by entering the corresponding number for each action.