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
Quick Algorithm
- Display the menu options.
- Take input from the user.
- Check the choice using if-elif-else.
- Perform the selected operation.
- 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.