Basic python def function and recall for a text menu

When working with Python, it is common to come across situations where you need to create a text menu that allows users to select different options. In this article, we will explore three different ways to solve this problem using Python’s def function and recall.

Option 1: Using if-elif-else statements

One way to create a text menu is by using if-elif-else statements. This approach involves defining a function that displays the menu options and prompts the user for input. Based on the user’s choice, the function then executes the corresponding code block.

def text_menu():
    print("1. Option 1")
    print("2. Option 2")
    print("3. Option 3")
    
    choice = input("Enter your choice: ")
    
    if choice == "1":
        print("You selected Option 1")
        # Code for Option 1
    elif choice == "2":
        print("You selected Option 2")
        # Code for Option 2
    elif choice == "3":
        print("You selected Option 3")
        # Code for Option 3
    else:
        print("Invalid choice")
        # Code for handling invalid choice

This approach is straightforward and easy to understand. However, it can become cumbersome if there are many menu options or if the code for each option is complex.

Option 2: Using a dictionary

Another approach is to use a dictionary to map the menu options to their corresponding functions. This allows for a more concise and scalable solution.

def option1():
    print("You selected Option 1")
    # Code for Option 1

def option2():
    print("You selected Option 2")
    # Code for Option 2

def option3():
    print("You selected Option 3")
    # Code for Option 3

def text_menu():
    menu_options = {
        "1": option1,
        "2": option2,
        "3": option3
    }
    
    print("1. Option 1")
    print("2. Option 2")
    print("3. Option 3")
    
    choice = input("Enter your choice: ")
    
    selected_option = menu_options.get(choice)
    
    if selected_option:
        selected_option()
    else:
        print("Invalid choice")
        # Code for handling invalid choice

This approach allows for a more modular and maintainable code. Adding new menu options or modifying existing ones is as simple as defining a new function and updating the dictionary.

Option 3: Using a class

A third option is to use a class to encapsulate the menu functionality. This approach provides even more flexibility and allows for additional features such as error handling and input validation.

class TextMenu:
    def __init__(self):
        self.menu_options = {
            "1": self.option1,
            "2": self.option2,
            "3": self.option3
        }
    
    def option1(self):
        print("You selected Option 1")
        # Code for Option 1

    def option2(self):
        print("You selected Option 2")
        # Code for Option 2

    def option3(self):
        print("You selected Option 3")
        # Code for Option 3

    def display_menu(self):
        print("1. Option 1")
        print("2. Option 2")
        print("3. Option 3")
        
        choice = input("Enter your choice: ")
        
        selected_option = self.menu_options.get(choice)
        
        if selected_option:
            selected_option()
        else:
            print("Invalid choice")
            # Code for handling invalid choice

text_menu = TextMenu()
text_menu.display_menu()

This approach provides a more object-oriented solution. It allows for better organization of the code and separation of concerns. Additionally, it enables the use of class methods and attributes to handle more complex scenarios.

After exploring these three options, it is clear that the best approach depends on the specific requirements of your project. If simplicity and readability are the main concerns, option 1 using if-elif-else statements may be the most suitable. However, if scalability and maintainability are important, options 2 and 3 using a dictionary or a class respectively provide more flexibility and modularity.

Rate this post

6 Responses

  1. Option 3: Using a class seems fancy, but why complicate things when if-elif-else works just fine? #KeepItSimple

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents