python
  1. python-menu

Python Menu

Syntax

# Sample code for creating a menu in Python

def menu():
    print("Welcome to my Python menu!")
    print("1. Option 1")
    print("2. Option 2")
    print("3. Option 3")
    print("4. Quit")

    choice = input("Enter your choice: ")

    if choice == "1":
        option1()
    elif choice == "2":
        option2()
    elif choice == "3":
        option3()
    elif choice == "4":
        quit()
    else:
        print("Invalid choice!")
        menu()
        
def option1():
    print("You chose option 1!")
    menu() # Going back to the main menu
    
def option2():
    print("You chose option 2!")
    menu() # Going back to the main menu
    
def option3():
    print("You chose option 3!")
    menu() # Going back to the main menu
    
menu() # Starting the menu

Example

def menu():
    print("Welcome to my Python menu!")
    print("1. Option 1")
    print("2. Option 2")
    print("3. Option 3")
    print("4. Quit")

    choice = input("Enter your choice: ")

    if choice == "1":
        option1()
    elif choice == "2":
        option2()
    elif choice == "3":
        option3()
    elif choice == "4":
        quit()
    else:
        print("Invalid choice!")
        menu()
        
def option1():
    print("You chose option 1!")
    menu() # Going back to the main menu
    
def option2():
    print("You chose option 2!")
    menu() # Going back to the main menu
    
def option3():
    print("You chose option 3!")
    menu() # Going back to the main menu
    
menu() # Starting the menu

Output

Welcome to my Python menu!
1. Option 1
2. Option 2
3. Option 3
4. Quit
Enter your choice: 1
You chose option 1!
Welcome to my Python menu!
1. Option 1
2. Option 2
3. Option 3
4. Quit
Enter your choice: 4

Explanation

This is a code snippet that demonstrates how to create a simple menu in Python. We create a function called menu that prints out the menu options and waits for user input. Based on the user input, we call different functions to execute the corresponding options.

The option1, option2, and option3 functions can be replaced with any other functions that you would like to execute for those menu options.

If the user enters an invalid choice, we print an error message and call the menu function again to allow them to make a valid choice.

The menu will continue to run until the user chooses to quit.

Use

You can use this menu code in your own program to provide a user-friendly interface for users to interact with your program. Simply replace the option1, option2, and option3 functions with the functions specific to your program, and update the menu options and messages as needed.

Important Points

  • The menu function must be called to start the menu.
  • Each menu option should call a specific function to execute the corresponding action.
  • Error checking should be included to handle invalid user input.
  • The menu should only end when the user chooses to quit.

Summary

A Python menu is a user-friendly way to offer options to the users of your program. By following the syntax and example above, you can create a menu with customized options and functions specific to your program's needs.

Published on: