Lab Description:
In this lab we were asked to do something similar to Lab 2. We had to create a user friendly menu in which the user had the options to compute mathematical operations. To create these operations we had to create functions that would meet the criteria for these operations. One example was that we had to make an option to find the sum of two integers (x and y). We made a function named Sum with an argument of x+y. This made them add to each other and then we later asked the user to input certain values to those integers by using the “input” command to ask the user what the values are for the integers. We did this for each of the different mathematical operations needed for this lab. We then made an option to “Exit”. This command let the user terminate the program which in turn told the program to end. Using the same format for the mathematical operations and an “if” statement we said if we do not choose any of the mathematical operations it will exit. Lastly we put all of these operations in a “while” statement which continues to run the program until all operations are met.
Code:
def Sum(x,y): print(x+y) def Subtract(x,y): print(x-y) def Multiply(x,y): print(x*y) def Divide(x,y): print(x/y) def Remainder(x,y): print(x%y) def Compare(x,y): if x>y: print('%d is larger than %d'%(x,y)) elif x==y: print('%d is equal to %d'%(x,y)) else: print('%d is less than %d'%(x,y)) def menu(): print('Welcome to Lab#3') print('1-Sum numbers') print('2-Subtract numbers') print('3-Multiply numbers') print('4-Divide numbers') print('5-Remainder of numbers') print('6-Compare numbers') print('7-Exit') choice= '' while choice != '7': choice=input('What do you want to do?') print (choice) if choice== '1': x = input('What is the first integer \n') y = input('What is the second integer \n') x= int(x) y= int(y) Sum(x,y) elif choice=='2': x = input('What is the first integer \n') y = input('What is the second integer \n') x= int(x) y= int(y) Subtract(x,y) elif choice=='3': x = input('What is the first integer \n') y = input('What is the second integer \n') x= int(x) y= int(y) Multiply(x,y) elif choice=='4': x = input('What is the first integer \n') y = input('What is the second integer \n') x= int(x) y= int(y) Divide(x,y) elif choice=='5': x = input('What is the first integer \n') y = input('What is the second integer \n') x= int(x) y= int(y) Remainder(x,y) elif choice=='6': x = input('What is the first integer \n') y = input('What is the second integer \n') x= int(x) y= int(y) Compare(x,y) elif choice=='7': print('good bye') else: print ('Please choose menu items 1-7') menu()