Menu-driven programs in Python are programs that provide a user with a set of options or actions to choose from through a menu interface. These options are usually displayed as a list of numbered items, and the user can select an item by entering the corresponding number.
Here is an example of a menu-driven program in Python that allows a user to perform some basic math operations:
def add(x, y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): return x / y print("Select operation.") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide") while True: choice = input("Enter choice(1/2/3/4): ") if choice in ('1', '2', '3', '4'): num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) if choice == '1': print(num1, "+", num2, "=", add(num1, num2)) elif choice == '2': print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '3': print(num1, "*", num2, "=", multiply(num1, num2)) elif choice == '4': print(num1, "/", num2, "=", divide(num1, num2)) break else: print("Invalid Input")
In this example, the program displays a menu with four options: Add, Subtract, Multiply, and Divide. The user is prompted to enter a choice by selecting a number from the list. If the choice is valid, the program prompts the user to enter two numbers and performs the selected operation on those numbers. If the choice is not valid, the program displays an error message and prompts the user to try again.
This is a simple example of a menu-driven program in Python, but the same principles can be applied to more complex programs with more options and actions.
Calculate the Parameter and Area of different Shapes using functions:
Here are examples of functions that calculate the parameter and area of different shapes in Python:
- Rectangle:
def rectangle_parameter(length, width): return 2 * (length + width) def rectangle_area(length, width): return length * width
- Circle:
import math def circle_parameter(radius): return 2 * math.pi * radius def circle_area(radius): return math.pi * radius**2
- Triangle:
def triangle_parameter(side1, side2, side3): return side1 + side2 + side3 def triangle_area(base, height): return 0.5 * base * height
- Square:
def square_parameter(side): return 4 * side def square_area(side): return side ** 2
These functions take the necessary parameters as input and return the calculated values for the parameter or area of the given shape. You can use these functions to calculate the parameter and area of different shapes in your programs.
Here is an example of how you can use these functions:
length = 5 width = 10 print("Rectangle Parameter:", rectangle_parameter(length, width)) print("Rectangle Area:", rectangle_area(length, width)) radius = 3 print("Circle Parameter:", circle_parameter(radius)) print("Circle Area:", circle_area(radius)) side1 = 3 side2 = 4 side3 = 5 print("Triangle Parameter:", triangle_parameter(side1, side2, side3)) print("Triangle Area:", triangle_area(base, height)) side = 6 print("Square Parameter:", square_parameter(side)) print("Square Area:", square_area(side))
This program will output the parameter and area of a rectangle, circle, triangle, and square based on the input parameters provided to the respective functions.
Menu-Driven Program to create a simple calculator:
Here is an example of a menu-driven program in Python to create a simple calculator:
def add(x, y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): return x / y print("Calculator Menu") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") while True: choice = input("Enter choice (1/2/3/4): ") if choice in ('1', '2', '3', '4'): num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) if choice == '1': print(num1, "+", num2, "=", add(num1, num2)) elif choice == '2': print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '3': print(num1, "*", num2, "=", multiply(num1, num2)) elif choice == '4': print(num1, "/", num2, "=", divide(num1, num2)) break else: print("Invalid Input")
In this program, the user is presented with a menu that allows them to select one of four arithmetic operations to perform: addition, subtraction, multiplication, or division. The user is prompted to enter their choice and then two numbers to perform the operation on.
The program then calls the appropriate function based on the user’s choice and outputs the result of the calculation. If the user enters an invalid choice, the program displays an error message and prompts the user to try again.
You can customize this program to add more arithmetic operations or provide a more user-friendly interface for the calculator.
Menu-Driven Program to create a Phone Directory:
Here’s an example of a menu-driven program in Python to create a phone directory:
phone_directory = {} def add_contact(name, number): phone_directory[name] = number print("Contact added successfully.") def remove_contact(name): if name in phone_directory: del phone_directory[name] print("Contact removed successfully.") else: print("Contact not found.") def lookup_contact(name): if name in phone_directory: print("Name:", name) print("Number:", phone_directory[name]) else: print("Contact not found.") def display_contacts(): if len(phone_directory) == 0: print("No contacts found.") else: print("Phone Directory:") for name, number in phone_directory.items(): print(name, ":", number) print("Phone Directory Menu") print("1. Add Contact") print("2. Remove Contact") print("3. Lookup Contact") print("4. Display Contacts") while True: choice = input("Enter choice (1/2/3/4): ") if choice == '1': name = input("Enter name: ") number = input("Enter number: ") add_contact(name, number) elif choice == '2': name = input("Enter name: ") remove_contact(name) elif choice == '3': name = input("Enter name: ") lookup_contact(name) elif choice == '4': display_contacts() else: print("Invalid Input")
In this program, the user is presented with a menu that allows them to add a contact, remove a contact, lookup a contact, or display all contacts in the directory. The program stores the phone directory as a dictionary with the contact name as the key and the phone number as the value.
The program then calls the appropriate function based on the user’s choice and performs the corresponding action. If the user enters an invalid choice, the program displays an error message and prompts the user to try again.
You can customize this program to add more features or provide a more user-friendly interface for the phone directory. For example, you could allow users to edit contact information, search for contacts by phone number, or save the phone directory to a file.
Conclusion:
In this session, we looked at how to create menu-driven programs in Python. We covered examples of creating programs to calculate parameters and areas of different shapes, create a simple calculator, and create a phone directory.
Menu-driven programs are a popular way to create user-friendly interfaces that allow users to interact with programs by selecting from a menu of options. By using functions to perform different actions, we can create modular code that is easier to maintain and update.
By customizing the examples provided, you can create your own menu-driven programs to perform a variety of tasks. The key is to design the program with the user in mind and provide clear and concise instructions for how to use it.