Lab Description:
This program is the same program I did in lab#2 but this time its incorporated in functions. It covers functions, Boolean expressions and while loop. Basically everything I have learnt so far. It wasn’t difficult splitting my previous program into functions but i had a little problem figuring out how the menu should work.
Code:
def sum_numbers(): x= int(input ("enter intger for x:")) y= int(input ("enter integer for y: ")) print ("x+y= ",(x+y)) def subtract_numbers(): x= int(input ("enter intger for x:")) y= int(input ("enter integer for y: ")) print ("x-y= ",(x-y)) def multiply_numbers(): x= int(input ("enter intger for x:")) y= int(input ("enter integer for y: ")) print ("x*y= ",(x*y)) def divide_numbers(): x= int(input ("enter intger for x:")) y= int(input ("enter integer for y: ")) print ("x/y= ",(x/y)) def remainder(): x= int(input ("enter intger for x:")) y= int(input ("enter integer for y: ")) if x==0 or y==0: print ("Math Error. x or y can not be 0") else: print ("x%y= ",(x%y)) def compare(): x= int(input ("enter intger for x:")) y= int(input ("enter integer for y: ")) if xy: print ("x is greater than y") else: print ("x is equal to y") print (" WELCOME!") print ("") print ("Select an option") print ("1- Sum of numbers") print ("2- Subtraction of Numbers") print ("3- Multiplication of Numbers") print ("4- Division of numbers") print ("5- Remainder of numbers") print ("6- Compare Numbers") print ("7- Exit") print ("") x= int(input("Choice: ")) if x==1: sum_numbers() elif x==2: subtract_numbers() elif x==3: multiply_numbers() elif x==4: divide_numbers() elif x==5: remainder() elif x==6: compare() if x==7: exit while x!=7: print ("") print ("Select an option") print ("1- Sum of numbers") print ("2- Subtract Numbers") print ("3- Multiply Numbers") print ("4- Dvivde numbers") print ("5- Remainder of numbers") print ("6- Compare Numbers") print ("7- Exit") print ("") x= int(input("Choice: ")) if x==1: sum_numbers() elif x==2: subtract_numbers() elif x==3: multiply_numbers() elif x==4: divide_numbers() elif x==5: remainder() elif x==6: compare() if x==7: break
Screenshot:
By C.k Obieyisi 3/9/2013