Function to Manage Task Database - Python Programming Exercise

In this exercise, you will develop a Python program to enhance the "tasks database" by dividing it into multiple functions for better organization and readability. This exercise is perfect for practicing function definition, modular programming, and code organization in Python. By implementing this program, you will gain hands-on experience in handling function definitions, modular programming, and code organization in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Function To Manage Task Database

 Objective

Develop a Python program to enhance the "tasks database" by dividing it into multiple functions for better organization and readability.

 Example Python Exercise

 Copy Python Code
# Global list to store tasks
tasks = []

# Function to display the menu options
def show_menu():
    print("\nTask Manager")
    print("1. Add Task")
    print("2. View Tasks")
    print("3. Remove Task")
    print("4. Exit")

# Function to add a task
def add_task():
    # Prompt the user to input a task description
    task = input("Enter the task description: ")
    tasks.append(task)  # Add the task to the list
    print(f"Task '{task}' added successfully.")  # Confirm that the task was added

# Function to view all tasks
def view_tasks():
    if tasks:
        # If there are tasks, display them
        print("\nTasks:")
        for i, task in enumerate(tasks, start=1):
            print(f"{i}. {task}")
    else:
        # If no tasks, inform the user
        print("No tasks available.")

# Function to remove a task
def remove_task():
    if tasks:
        view_tasks()  # Show the current tasks
        try:
            # Prompt the user to select a task to remove
            task_index = int(input("Enter the number of the task to remove: ")) - 1
            if 0 <= task_index < len(tasks):  # Check if the input is valid
                removed_task = tasks.pop(task_index)  # Remove the selected task
                print(f"Task '{removed_task}' removed successfully.")  # Confirm removal
            else:
                print("Invalid task number.")  # Handle invalid task number
        except ValueError:
            print("Please enter a valid number.")  # Handle non-numeric input
    else:
        print("No tasks available to remove.")  # If no tasks, inform the user

# Main function to run the program
def main():
    while True:
        show_menu()  # Display the menu

        try:
            # Prompt the user to choose an option
            choice = int(input("Choose an option (1-4): "))
            if choice == 1:
                add_task()  # Add a task if option 1 is selected
            elif choice == 2:
                view_tasks()  # View tasks if option 2 is selected
            elif choice == 3:
                remove_task()  # Remove a task if option 3 is selected
            elif choice == 4:
                print("Exiting program.")  # Exit the program if option 4 is selected
                break
            else:
                print("Invalid option. Please choose between 1 and 4.")  # Handle invalid options
        except ValueError:
            print("Please enter a valid number.")  # Handle non-numeric input

# Run the main function
if __name__ == "__main__":
    main()

 Output

Task Manager
1. Add Task
2. View Tasks
3. Remove Task
4. Exit
Choose an option (1-4): 1
Enter the task description: Buy groceries
Task 'Buy groceries' added successfully.

Task Manager
1. Add Task
2. View Tasks
3. Remove Task
4. Exit
Choose an option (1-4): 2

Tasks:
1. Buy groceries

Task Manager
1. Add Task
2. View Tasks
3. Remove Task
4. Exit
Choose an option (1-4): 3
Enter the number of the task to remove: 1
Task 'Buy groceries' removed successfully.

Task Manager
1. Add Task
2. View Tasks
3. Remove Task
4. Exit
Choose an option (1-4): 4
Exiting program.

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Function to Find Maximum Value in an Array

    In this exercise, you will develop a Python program containing a function that accepts a list of floating-point numbers as input and returns the maximum value within ...

  •  Iterative Implementation of the Factorial Function

    In this exercise, you will develop a Python program that calculates the factorial of a given number using an iterative (non-recursive) method. This exercise is...

  •  Function to Print Title

    In this exercise, you will develop a Python program with a function called 'WriteTitle' that displays a text in uppercase, centered on the screen with extra spaces an...

  •  Main Function Output Value

    In this exercise, you will develop a Python program that prompts the user to provide a title via the command line (using the previously defined WriteTitle function). ...

  •  Function to Calculate DV

    In this exercise, you will develop a Python program that defines a function to count the number of numeric digits and vowels in a given string. The function should ta...

  •  Function to Verify Alphabetic Characters

    In this exercise, you will develop a Python program that includes a function to determine if a given character is alphabetic (from A to Z). This exercise is pe...