Menu Driven Program in Python

Introduction to Menu-Driven Program

A menu driven program in Python is a program that takes input from a user by displaying a list of options and allows users to choose the input from the list of options.

A simple example of a menu-driven program is an ATM( Automated teller machine). In the case of a transaction, it takes input from the user by the keypress and processes the transaction at the background. Similarly one can think an example of a coffee vending machine, Fast food menu, TV Remote control etc. There are several advantages of menu driven programs in real world.

Advantages of Menu-Driven Program

The major advantage of menu-driven systems are:

  • Simple and User-friendly interface
  • Best of Novice or Beginners
  • Not necessary to remember the long list of commands
  • Self-explanatory

Now that we know menu-driven let us look at some examples of implementing in the Python programming language.

In the following menu-driven program, we are going to build a simple calculator in Python, which displays a list of choices(Addition, Subtract, Multiply and Divide) to the user with the help of an infinite while loop.

Based on the user choice, one of the user-defined functions will be called with the help of the if-else condition. For example, if the user enters one addition, the function will be called, and if the user enters four, the divide function is called.

In Order to exit the menu-driven program the break statement is called on user input (choice 5), which breaks out of the while loop and exits the program.

If user enters an invalid choice we will display a validation message to rectify. Alternatively the program can be further improved by handling the exceptions if user enters invalid input.

# Adding two numbers
def add(a, b):  
    sum = a + b  
    print(a, "+", b, "=", sum)  
  
# Subtract two numbers
def subtract(a, b):  
    difference = a - b  
    print(a, "-", b, "=", difference)  
  
# Multiply two numbers
def multiply(a, b):  
    product = a * b  
    print(a, "x", b, "=", product)  
  
# Divide two numbers
def divide(a, b):  
    division = a / b  
    print(a, "/", b, "=", division)  
  
# Menu Driven Heading 
print("WELCOME TO CALCULATOR\n")  
  
# using the while loop to print menu list  
while True:  
    print("MENU")  
    print("1. Addition of two Numbers")  
    print("2. Difference between two Numbers")  
    print("3. Multiplication of two Numbers")  
    print("4. Division of two Numbers")  
    print("5. Exit")  
    users_choice = int(input("\nEnter your Choice: "))  
  
# based on the users choice the relevant method is called
    if users_choice == 1:  
        print( "\nPERFORMING ADDITION\n")  
        a = int( input("Enter First Number: "))  
        b = int( input("Enter Second Number: "))  
        add(a, b)  
  
    elif users_choice == 2:  
        print( "\nPERFORMING SUBTRACTION\n")  
        a = int( input("Enter First Number: "))  
        b = int( input("Enter Second Number: "))  
        subtract(a, b)  

    elif users_choice == 3:  
        print( "\nPERFORMING MULTIPLICATION\n")  
        a = int( input("Enter First Number: "))  
        b = int( input("Enter Second Number: "))  
        multiply(a, b)  

  
    elif users_choice == 4:  
        print( "\nPERFORMING DIVISION\n")  
        a = int( input("Enter First Number: "))  
        b = int( input("Enter Second Number: "))  
        divide(a, b)  

  
  # exit the while loop
    elif users_choice == 5:  
        break  
      
    else:  
        print( "Please enter a valid Input from the list")  

Output

WELCOME TO CALCULATOR

MENU
1. Addition of two Numbers
2. Difference between two Numbers
3. Multiplication of two Numbers
4. Division of two Numbers
5. Exit

Enter your Choice: 1

PERFORMING ADDITION

Enter First Number: 2
Enter Second Number: 4
2 + 4 = 6
MENU
1. Addition of two Numbers
2. Difference between two Numbers
3. Multiplication of two Numbers
4. Division of two Numbers
5. Exit

Enter your Choice: 2

PERFORMING SUBTRACTION

Enter First Number: 4
Enter Second Number: 1
4 - 1 = 3
MENU
1. Addition of two Numbers
2. Difference between two Numbers
3. Multiplication of two Numbers
4. Division of two Numbers
5. Exit

Enter your Choice: 3

PERFORMING MULTIPLICATION

Enter First Number: 4
Enter Second Number: 7
4 x 7 = 28
MENU
1. Addition of two Numbers
2. Difference between two Numbers
3. Multiplication of two Numbers
4. Division of two Numbers
5. Exit

Enter your Choice: 4

PERFORMING DIVISION

Enter First Number: 6
Enter Second Number: 2
6 / 2 = 3.0
MENU
1. Addition of two Numbers
2. Difference between two Numbers
3. Multiplication of two Numbers
4. Division of two Numbers
5. Exit

Enter your Choice: 5
2 comments
    1. Pretty simple solution is we can wrap it up in a function and when the user enters invalid menu option we can print a message stating “You have entered invalid Options” and call back the same method which brings up the menu.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python String Lower()

Python String lower()

Table of Contents Hide lower() Syntax lower() Parameterslower() Return ValueExample 1: Convert a string to lowercaseExample 2: String with Alphanumeric charactersExample 3: How to check if two strings are same…
View Post
Convert Letters To Numbers In Python

Convert Letters to Numbers in Python

Table of Contents Hide Convert Letters to Numbers in PythonUsing ord() MethodUsing list comprehensionConclusion Using the ord() method, we can convert letters to numbers in Python. The ord() method returns…
View Post
Python Chr() A Step-By-Step Guide

Python chr(): A Step-By-Step Guide

The chr() function takes an integer (representing Unicode) as an input argument and returns a string representing a character. Syntax – chr(num) Parameter and Return Value The chr() function can…
View Post
Python Bytes()

Python bytes()

Table of Contents Hide bytes() Syntax bytes() Parametersbytes() Return ValueExample 1: Create a byte of a given integer sizeExample 2: Convert string to bytesExample 3: Convert iterable list to bytesExample 4:…
View Post