Menu driven Program in Python using while loop

Learn how to create a menu-driven program in Python using a while loop to calculate the area of circle, rectangle, and square with complete code

A Menu Driven Program in Python is a program that displays multiple options to the user. Based on the user's choice, the program performs a specific task. This program is very important for Class 12 CBSE board practical exams.

What is a Menu Driven Program?

A menu-driven program allows users to select from different options displayed on the screen. The program then executes the operation based on the selected option using if-elif-else statements and loops.

Menu Driven Program in Python Using While Loop

In this example, we calculate:

  • Area of Circle
  • Area of Rectangle
  • Area of Square
  • Exit Program

Python Code


while True:
    print("Menu Driven Program")
    print("1. Area of Circle")
    print("2. Area of Rectangle")
    print("3. Area of Square")
    print("4. Exit")

    choice = int(input("Enter your choice: "))

    if choice == 1:
        radius = int(input("Enter radius of Circle: "))
        print("Area of Circle:", 3.14 * radius * radius)

    elif choice == 2:
        length = int(input("Enter length of Rectangle: "))
        breadth = int(input("Enter breadth of Rectangle: "))
        print("Area of Rectangle:", length * breadth)

    elif choice == 3:
        side = int(input("Enter side of Square: "))
        print("Area of Square:", side * side)

    elif choice == 4:
        print("Exiting Program...")
        break

    else:
        print("Wrong Choice")

    repeat = input("Do you want to continue? (y/n): ")
    if repeat.lower() == 'n':
        break

Sample Output

Menu Driven Program
1. Area of Circle
2. Area of Rectangle
3. Area of Square
4. Exit
Enter your choice: 3
Enter side of Square: 5
Area of Square: 25
Menu Driven Program in Python using while loop example output

Quick Algorithm

  1. Display the menu options.
  2. Take input from the user.
  3. Check the choice using if-elif-else.
  4. Perform the selected operation.
  5. Use break statement to exit.

Menu Driven Program in Python Using Functions

Now let’s implement the same program using Python functions.

Python Code Using Functions


def circle(radius):
    print("Area of Circle:", 3.14 * radius * radius)

def rectangle(length, breadth):
    print("Area of Rectangle:", length * breadth)

def square(side):
    print("Area of Square:", side * side)

while True:
    print("Menu Driven Program")
    print("1. Area of Circle")
    print("2. Area of Rectangle")
    print("3. Area of Square")
    print("4. Exit")

    choice = int(input("Enter your choice: "))

    if choice == 1:
        radius = int(input("Enter radius of Circle: "))
        circle(radius)

    elif choice == 2:
        length = int(input("Enter length of Rectangle: "))
        breadth = int(input("Enter breadth of Rectangle: "))
        rectangle(length, breadth)

    elif choice == 3:
        side = int(input("Enter side of Square: "))
        square(side)

    elif choice == 4:
        print("Exiting Program...")
        break

    else:
        print("Wrong Choice")

Why This Program is Important for CBSE Class 12?

  • Commonly asked in practical exams
  • Tests understanding of loops
  • Checks conditional statements knowledge
  • Evaluates function usage

Frequently Asked Questions (FAQ)

What is a menu-driven program in Python?

A menu-driven program displays multiple options to users and performs operations based on their selection using loops and conditional statements.

How do you exit a menu-driven program?

We use the break statement inside the loop when the user selects the exit option.

Can we use functions in a menu-driven program?

Yes, functions make the program more modular, readable, and structured.

Conclusion: This menu-driven program in Python using while loop and functions is essential for beginners and Class 12 students. Practice this program thoroughly for board exams and practicals.

19 comments

  1. how can we make the loop run again to accept the input if input is "Wrong Choice"
    while True:
    dine=eval(input("Please type 1 for Drive-Thru ; 2 for Dine-in ; 3 for Carry-out/Delivery"))

    if dine==1:
    dine_type="Drive-Thru"

    elif dine==2:
    dine_type="Dine-in"

    elif dine==3:
    dine_type="Carry-out/Delivery"
    else:
    print("Wrong Choice")
  2. is def compulsary for structure progamming?
    1. No, it's not compulsory to use functions for structure programming.

      We break complex programs into small tasks using functions.

      To organize our programs in python we use functions.
  3. Develop menu driven program for library book management without using def function
    1. Ok
  4. Very informative and educative site
    1. Thanks 😊
  5. What will be the flowchart for the above program?
    1. We will be uploading it soon. Please text us on instagram for the same.
  6. Great website
    1. Thanks ❤️
  7. Thanks for this post, great content.
  8. Welcome, please fill our feedback form
  9. I have an assignment
  10. create a menu driven for tuple random and str if the choice is selected the functions of the particular data type should be shown in a menu driven
    1. Sure
  11. create a menu driven for tuple random and str if the choice is selected the functions of the particular data type should be shown in a menu driven using user define function
  12. It is looping again after wrong choice also
Please do not enter any spam link in the comment box.