In this lab we start using functions. This program displays a menu to the user with the following options: 1- Sum numbers 2- Subtract numbers 3- Multiply numbers 4- Divide numbers 5- Remainder of numbers 6- Compare numbers 7- Exit. After the user makes a selection, the program ask for two numbers x and y and perform the selected operation and show the result.
def addition (x,y):
add = x+y
return add
def subtract (x,y):
z2 = x-y
return z2
def multiply (x,y):
z3 = (x * y)
return z3
def divide (x,y):
z4 = (x / y)
return z4
def remainder (x,y):
z5 = (x % y)
return z5
print (‘————– SELECTIONS —————‘)
print (‘enter 1 to add the numbers’)
print (‘enter 2 to subtract the numbers’)
print (‘enter 3 to multiply the numbers’)
print (‘enter 4 to divide the numbers’)
print (‘enter 5 to show the numbers remainder’)
print (‘enter 6 to show if x is less than,more than or equal to y’)
print (‘enter 7 to exit’)
print (‘———————————————————‘)
x = float(input(‘please enter number x: ‘))
y = float (input(‘please enter number y: ‘))
while (x!=0) and (y!=0):
z = float(input(‘enter your selection: ‘))
if z == 1:
print(‘the sum of’,x, ‘and’, y, ‘is’, addition(x,y))
elif z == 2:
print(‘the difference of’,x, ‘and’, y, ‘is’, subtract (x,y))
elif z ==3:
print(‘the product of’,x, ‘and’, y, ‘is’,multiply (x,y))
elif z ==4:
print(‘the quotient of’,x, ‘and’, y, ‘is’, divide (x,y))
elif z ==5:
print(‘the remainder of’,x, ‘and’, y, ‘is’, remainder (x,y))
elif z == 6:
if x<y:
print (‘x is less than y’)
elif x>y:
print (‘x is more than y’)
else:
print (‘x is equal to y’)
elif z == 7:
print (‘program finish’)
else:
print (‘please enter a valid selection’)
print (‘———————————————————‘)
x = float (input(‘please input number x: ‘))
y = float (input(‘please input number y: ‘))