1. Introduction
A Todo application is a basic project for beginners that helps understand CRUD (Create, Read, Update, Delete) operations. This tutorial will guide you through creating a simple console-based Todo app in Python that allows users to manage their tasks. Users can add a new task, view all tasks, update an existing task, and delete a task.
2. Program Steps
1. Initialize a list to store tasks.
2. Display a menu for the user to choose an operation.
3. Implement functions for each CRUD operation.
4. Take user input to select which operation to perform.
5. Repeat the menu until the user decides to exit the application.
3. Code Program
# Step 1: Initialize the list to store tasks
tasks = []
# Function to display menu and handle user input
def display_menu():
print("\nTodo App")
print("1. Add Task")
print("2. View Tasks")
print("3. Update Task")
print("4. Delete Task")
print("5. Exit")
choice = input("Enter your choice: ")
return choice
# CRUD Operations
# Step 3a: Add a new task
def add_task():
task = input("Enter the task: ")
tasks.append(task)
print("Task added.")
# Step 3b: View all tasks
def view_tasks():
if tasks:
print("\nTasks:")
for index, task in enumerate(tasks, start=1):
print(f"{index}. {task}")
else:
print("No tasks found.")
# Step 3c: Update an existing task
def update_task():
view_tasks()
if tasks:
task_number = int(input("Enter task number to update: "))
if 1 <= task_number <= len(tasks):
new_task = input("Enter the new task: ")
tasks[task_number - 1] = new_task
print("Task updated.")
else:
print("Invalid task number.")
# Step 3d: Delete a task
def delete_task():
view_tasks()
if tasks:
task_number = int(input("Enter task number to delete: "))
if 1 <= task_number <= len(tasks):
del tasks[task_number - 1]
print("Task deleted.")
else:
print("Invalid task number.")
# Main program loop
while True:
user_choice = display_menu()
if user_choice == "1":
add_task()
elif user_choice == "2":
view_tasks()
elif user_choice == "3":
update_task()
elif user_choice == "4":
delete_task()
elif user_choice == "5":
print("Exiting Todo App.")
break
else:
print("Invalid choice. Please choose again.")
Output:
Todo App 1. Add Task 2. View Tasks 3. Update Task 4. Delete Task 5. Exit Enter your choice: 1 Enter the task: learn python Task added. Todo App 1. Add Task 2. View Tasks 3. Update Task 4. Delete Task 5. Exit Enter your choice: 2 Tasks: 1. learn python Todo App 1. Add Task 2. View Tasks 3. Update Task 4. Delete Task 5. Exit Enter your choice: 3 Tasks: 1. learn python Enter task number to update: 1 Enter the new task: learn python in-depth Task updated. Todo App 1. Add Task 2. View Tasks 3. Update Task 4. Delete Task 5. Exit Enter your choice: 4 Tasks: 1. learn python in-depth Enter task number to delete: 1 Task deleted. Todo App 1. Add Task 2. View Tasks 3. Update Task 4. Delete Task 5. Exit Enter your choice: 2 No tasks found. Todo App 1. Add Task 2. View Tasks 3. Update Task 4. Delete Task 5. Exit Enter your choice: 5 Exiting Todo App.
Explanation:
1. The program begins by initializing an empty list named tasks to store the todo tasks.
2. The display_menu function prints the operations menu and returns the user's choice. This function is called repeatedly in the main program loop to allow the user to perform multiple operations.
3. For each CRUD operation, a corresponding function (add_task, view_tasks, update_task, delete_task) is defined. These functions handle adding a new task to the list, displaying all tasks, modifying an existing task based on user input, and deleting a task, respectively.
4. User input is taken to determine which operation to perform, and the appropriate function is called based on the user's choice.
5. The view_tasks function is also called within update_task and delete_task to display the list of tasks and help the user select which task to update or delete.
Comments
Post a Comment
Leave Comment