Write a phython program to display a menu with the following options: (1) add, (2) subtract, (3) multiply, (4) divide (5) mod, and (6) do nothing. If the user enters something else (except 1-6), the program should display an error message. Otherwise, it should ask the user for two numbers, perform the calculation, and display the result.
Solution
code
# This function finds the addition of two numbers
def add(x, y):
return x + y
# This function finds the subtraction of two numbers
def subtract(x, y):
return x - y
# This function finds the multiplication of two numbers
def multiply(x, y):
return x * y
# This function finds the division of two numbers
def divide(x, y):
return x / y
# This function finds the mod of two numbers
def mod(x, y):
return x % y
# This function do nothing
def donothing(x,y):
return null
print("Select operation.")
print("(1) add")
print("(2) subtract")
print("(3) multiply")
print("(4) Divide")
print("(5) mod")
print("(6) do nothing")
# Take input from the user
choice = input("Please enter choice(1/2/3/4/5/6):")
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the 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))
elif choice == '5':
print(num1,"%",num2,"=", mod(num1,num2))
elif choice == '6':
print("Do nothing")
else:
print("Error :Invalid input")
Screenshot
Output
--
all the best
please upvote
Get Answers For Free
Most questions answered within 1 hours.