Menu-Driven Program to Create a Simple Calculator
Let’s create a simple calculator using Python that can perform basic arithmetic operations like addition, subtraction, multiplication, and division. This type of application demonstrates how to handle user input and make decisions in your code.
Here's a straightforward Python program that implements a basic calculator:
Python
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):
if y == 0:
return "Cannot divide by zero"
return x / y
def menu():
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("Result: ", add(num1, num2))
elif choice == '2':
print("Result: ", subtract(num1, num2))
elif choice == '3':
print("Result: ", multiply(num1, num2))
elif choice == '4':
print("Result: ", divide(num1, num2))
break
else:
print("Invalid Input")
menu()

You can also try this code with Online Python Compiler
Run Code
Output
Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
Enter choice(1/2/3/4): 1
Enter first number: 7
Enter second number: 22
Result: 29.0
This program first defines functions for each arithmetic operation. The menu function displays the available operations and takes user input. It then performs the chosen operation using the appropriate function and prints the result. The loop ensures that the user inputs a valid choice, and it repeats the prompt if the input is incorrect.
Menu-Driven Program to Insert & Delete Elements in a Single Dimension Array of Integers & Print the Array After Insertion & Deletion
Managing arrays is a common task in programming, involving adding and removing elements based on user input. Let's design a menu-driven program in Python that allows users to insert and delete integers in an array and then displays the updated array.
Here's how you can implement this in Python
Python
def display_array(arr):
print("Current Array:", arr)
def insert_element(arr, element, position):
arr.insert(position, element)
return arr
def delete_element(arr, position):
if position < len(arr):
arr.pop(position)
else:
print("Position out of range")
return arr
def array_menu():
arr = []
while True:
print("\n1. Insert Element")
print("2. Delete Element")
print("3. Display Array")
print("4. Exit")
choice = input("Enter choice (1/2/3/4): ")
if choice == '1':
element = int(input("Enter element to insert: "))
position = int(input("Enter position to insert element at: "))
arr = insert_element(arr, element, position)
display_array(arr)
elif choice == '2':
position = int(input("Enter position of element to delete: "))
arr = delete_element(arr, position)
display_array(arr)
elif choice == '3':
display_array(arr)
elif choice == '4':
print("Exiting program.")
break
else:
print("Invalid choice. Please select again.")
array_menu()

You can also try this code with Online Python Compiler
Run Code
Output
1. Insert Element
2. Delete Element
3. Display Array
4. Exit
Enter choice (1/2/3/4): 1
Enter element to insert: 2
Enter position to insert element at: 0
Current Array: [2]
1. Insert Element
2. Delete Element
3. Display Array
4. Exit
Enter choice (1/2/3/4): 1
Enter element to insert: 17
Enter position to insert element at: 2
Current Array: [2, 17]
1. Insert Element
2. Delete Element
3. Display Array
4. Exit
Enter choice (1/2/3/4): 3
Current Array: [2, 17]
1. Insert Element
2. Delete Element
3. Display Array
4. Exit
Enter choice (1/2/3/4): 4
Exiting program.
This program starts by initializing an empty array. The array_menu function offers options to insert an element, delete an element, display the array, or exit the program. Each option calls a specific function to perform the requested operation. The insert_element function inserts a new element at a specified position, delete_element removes the element at a given position if it exists, and display_array prints the current state of the array. This straightforward approach ensures that the user's intentions are handled accurately and the array is modified accordingly.
Frequently Asked Questions
What if I enter a position that is out of range for deletion?
If the position entered is out of range, the program will notify you with a "Position out of range" message and will not modify the array.
Can I insert elements at any position in the array?
Yes, you can insert elements at any position. If the position exceeds the current array length, Python will automatically extend the array and place the new element at the specified position.
How does this program handle invalid inputs?
The program continuously prompts for a valid choice until the user enters a correct command. For numerical inputs like position and element, the program expects an integer; entering a non-integer will raise an error.
Conclusion
In this article, we have learned how to create and manipulate menu-driven programs in Python. Starting with basic concepts, we explored calculating the perimeter and area of shapes, creating a simple calculator, and managing integer arrays through insertion and deletion. Each example demonstrated essential Python functionalities, including function definition, user input handling, and conditional logic. These programs are basics, which will help you in understanding how to structure code and interact with users effectively in your programming journey.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.