Description

In this lab, we now define a function (our program) with an specific name to make it easier to call and run it instead of writing everything once again, and we also gave the person more options to choose specifically an operation of preference. For the function,at the beginning of our program, we used the term “def” and named it how ever wepleased (it’s suggested to name it with something that relates to the function, to make it easier to remember, and other people to understand it.) In this case I chose to name my function “operations” after the name of my function i place  a set of parenthesis “( )” that would be our parameters, followed by “:” After we defined the function, I set my conditions using a while for a main menu that gives me two options; Enter a number and Exit. I used an “If” option == 1 then the it will enter to the loop that will now ask for an specific operation in the list where the selection is based on own preference, after the selection is made, now it will ask for two values, one for X and another for Y. The operation will be solved and will show the result follow by the second menu, asking for a new selection, if option 7 happens to be the choice, then the loop will exit and go back to the first (main) menu. In the first menu, if the selection happens to be 2, then the program will exit.

Code

 

def operations():
     option = 1
     option = input (“Menu :\n (1) Enter numbers\n (2) Exit \n”)
     while 1:
          if option == 1:
                    oper= input (“Menu :\n (1) Enter Sum\n (2) Enter Difference\n (3) Enter Product\n (4) Enter Division\n (5) Enter remainder\n (6) Enter Greater than, Less than, Equal to\n (7) Main Menu\n “)
                    if oper > 1 < 7:
                         def sum1(x,y):
                              print (x + y)
                         def diff(x,y):
                              print (x – y)
                         def mult(x,y):
                              print (x * y)
                         def div(x,y):
                              print (x / y)
                         def rem(x,y):
                              print (x % y)
                         def operdiff(x,y):
                             if x > y :
                                 print “X is greater than Y “
                             elif x < y:
                                 print “X is less than Y”
                             else:
                                 print “X is equal to Y”
                         if oper == 1:
                              x = input (“Enter x “)
                              y = input (“Enter y “)
                              print “The sum is: “
                              sum1(x,y)
                         elif oper == 2:
                              x = input (“Enter x “)
                              y = input (“Enter y “)
                              print “The difference is: “
                              diff(x,y)
                         elif oper == 3:
                              x = input (“Enter x “)
                              y = input (“Enter y “)
                              print “The product is: “
                              mult(x,y)
                         elif oper == 4 :
                              x = input (“Enter x “)
                              y = input (“Enter y “)
                              print “The quotation is: “
                              div(x,y)
                         elif oper == 5 :
                              x = input (“Enter x “)
                              y = input (“Enter y “)
                              print “The remainder is: “
                              rem(x,y)
                         elif oper == 6:
                              x = input (“Enter x “)
                              y = input (“Enter y “)
                              operdiff(x,y)
                         else:
                              return operations()

 

Screenshot

Screen Shot 2013-11-07 at 9.10.28 AM