Description:
This Lab was a bit more complicated than the last lab in that there were clear needs for automated code. We had to make a program that would prompt the user for integers and perform operations with those integers, that we defined as “x” and “y”. User input was obtained using the “input()” command and various operators were used. Also, Input’s string value was converted to an integer value using “int()”. An interface was created as a function (main) to prompt the user to decide whether he/she wanted to repeat or quit. This was accomplished through conditional expressions. A conditional statement was also used to circumvent a ZeroDivision Error. The “print()” function was used to output all computations to the user. All math and output commands were defined inside “main” as conditions. Only the “restart or quit” command was run. This in effect created a loop that would have to be manually ended by the user.
Code:
def main(): choice = input('Do you want to run the program?\n Please enter y or n ...') if choice == 'n' or choice == 'N': quit elif choice == 'y' or choice == 'Y': #this prompts the user for the integers needed, x and y x = input('What is the first integer \n') y = input('What is the second integer \n') #this converts the inputed strings into integer values x = int(x) y = int(y) #this performs the functions of the code print(x+y,' is the sum of the two integers') print(x-y,' is the difference between two integers') print(x*y,' is the product of two integers') #this validates a zero division error if y==0: print('cannot divide by zero bro') else: print(x/y,'is the quotient of two integers') print(x%y,'is the remainder when dividing x by y') #this compares x and 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)) main() else: print('y or n, dummy...') main() main()
Screenshots: