Lab Three

demlab3paint

Lab 3 was an exercise in creating functions within Python. Lab 2 was rewritten using functions and a program was created to carry out an array of mathematical operations with a basic menu as an interactive user interface.

Program Script:

def add (x,y):
z = x+y
return z
def subtract (x,y):
z = x-y
return z
def multiply (x,y):
z = x*y
return z
def divide (x,y):
z = x/y
return z
def remainder (x,y):
z = x%y
return z
def xgy (x,y):
if (x>y):
return (‘X is greater than Y’)
elif (x<y):
return (‘X is less than Y’)
elif (x == y):
return (‘X is equal to Y’)
def print_menu ():
print (‘1. Addition’)
print (‘2. Subtraction’)
print (‘3. Multiplication’)
print (‘4. Division’)
print (‘5. Remainder’)
print (‘6. Comparision’)
print (‘7. Exit Program’)

print_menu ()
menu_item = input (‘Which operation would you like to perform?’)
menu_item = int (menu_item)

while (menu_item !=7):
x = input (‘What is your first value?’)
y = input (‘What is your second value?’)
x = int (x)
y = int (y)
if (menu_item == 1):
z= add (x,y)
print (z)
if (menu_item == 2):
subtract (x,y)
z= subtract (x,y)
print (z)
if (menu_item == 3):
multiply (x,y)
z = multiply(x,y)
print (z)
if (menu_item == 4):
divide (x,y)
z= divide (x,y)
print (z)
if (menu_item == 5):
remainder (x,y)
z= remainder (x,y)
print (z)
if (menu_item == 6):
xgy(x,y)
z= xgy (x,y)
print (z)
print_menu ()
menu_item = input (‘Which operation would you like to perform?’)
menu_item = int (menu_item)

Leave a Reply

Your email address will not be published. Required fields are marked *