#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Is my teacher teaching us well?


    Code:
    #Date: Nov. 20. 2012
    #Purpose: To write a modular program to simulate a simple calculator.
    #Accept two numbers from the user and give the user the option of adding,
    #subtracting, multiplying or dividing them. The program executes till the user
    #enters 0 for both numbers. Non numeric input should be ignored and an error
    #message to that effect should be displayed.
    
    #The menu for asking the operation to be done and returning the choice
    def showMenu():
        #Integer choice
    
        print("------------------ Menu -----------------------")
        print("1:  Add the numbers")
        print("2:  Subtract the numbers")
        print("3:  Multiply the numbers")
        print("4:  Divide the numbers")
        print("5:  Exit") 
        print("-----------------------------------------------")
    
        try:
            choice = int(input("Please enter your choice(1-5): "))
            #choice must be a value 1 through 5
            while (choice < 1 or choice > 5):
                print("You MUST enter a valid value")
                choice = int(input("Please enter your choice(1-5): "))
            return choice    
        except ValueError:
            print("Non numeric input ignored. Try again...")
        except:
            print("Input Error!!")
    ###########################################################################
    #This function adds two numbers and returns the sum        
    def addNumbers(n1,n2):
        #Real n1, n2
    
        return n1 +n2
    ###########################################################################
    #This function subtracts the smaller number from the bigger number and returns
    #the difference
    def subtractNumbers(n1,n2):
        #Real n1, n2
    
        if ( n1 > n2):
            return n1-n2
        else:
            return n2-n1
    
    ###########################################################################
    #This function returns the product of the two numbers        
    def multiplyNumbers(n1,n2):
        #Real n1, n2
    
        return n1 * n2
    ###########################################################################
    #This function divides the first number by the second number and returns the quotient        
    def divideNumbers(n1,n2):
        #Real n1, n2
    
        return n1/n2
    ###########################################################################
    def printResults(number1, number2, summation, sumFlag, difference, diffFlag, product, prodFlag, quotient, quoFlag):
        #Boolean sumFlag, diffFlag, prodFlag, quoFlag    
        #Real number1, number2, summation, difference,  product, quotient
    
        print("-------------------- O U T P U T ---------------------------")
        print("Number One           : %.2f" %number1)
        print("Number Two           : %.2f" %number2)
        if (sumFlag):
            print("Sum                  : %.2f" %summation)
        if (diffFlag):    
            print("Positive Difference  : %.2f" %difference)
        if (prodFlag):
            print("Product              : %.2f" %product)
        if(quoFlag):
            print("Quotient             : %.2f" %quotient)
        
        print("------------------------------------------------------------")
        return
    #######################################################################################
    #This function accepts a number from the user and returns it. It displays
    #an error message in case of non-numeric input. Non-numeric input is
    #ignored.
    def getNumber():
        #Real number
    
        while(True):
            try:
                number = float(input("Please enter a number: "))
                return number
            except ValueError:
                print("Non numeric input was provided. Try again...")
            except:
                print("Input error!!")
                
    ############################################################################## 
    
    #The main function hadles the input, processing and output
    def main():
        #Real number1, number2, summation, difference, product, quotient
        #Integer choice
        #Boolean sumFlag, diffFlag, prodFlag, quoFlag
       
        
        #get the two numbers
        number1 = getNumber()
        number2 = getNumber()
    
        #loop while both numbers are not 0
        while (number1 != 0 and number2 != 0):
            
            sumFlag= diffFlag = quoFlag= prodFlag = False
            summation=difference=product=quotient = 0
            
            #show a menu to the user to choose the operation
            choice = showMenu()
            while (choice != 5):
                if (choice ==1):
                    summation = addNumbers(number1, number2)
                    sumFlag = True
                elif (choice==2):
                    difference = subtractNumbers(number1, number2)
                    diffFlag = True
                elif(choice == 3):
                    product = multiplyNumbers(number1, number2)
                    prodFlag = True
                else:
                    quotient= divideNumbers(number1, number2)
                    quoFlag = True
    
                choice = showMenu()
                
    
            #print the results
            printResults(number1, number2, summation, sumFlag, difference, diffFlag, product, prodFlag, quotient, quoFlag)
            
            #get the next pair of numbers
            number1 = getNumber()
            number2 = getNumber()
    
        return
    #############end of main#########################################################
    main()
    I think they way she teaches is quite complex. What do you think?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    What part did your instructor write?
    Are you learning at a furious rate?
    Why don't you comment #middle of main?

    The assignment is uninspired. python is already a good calculator. (Click here to discover a great calculator.) A gui calculator has some interest, perhaps. Anyway, the task inspires useful notions. Verify input. Trap errors. Partition the problem into tiny manageable chunks.
    Last edited by b49P23TIvg; November 20th, 2012 at 03:08 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    19
    Rep Power
    0
    I think you're teacher is just fine. If you doubt too much you won't learn much.

    The style is not according to PEP8 style guidelines but, maybe she has her reasons. All that you see in that little script is basic but import stuff that you need to practice and get familiar with. It's actually not a complicated program. You will write much worse programs on you're own that are far more complex when you're still learning. That's what this is for, to save you the time and show you how a program is pieced together with functions that modular style.

IMN logo majestic logo threadwatch logo seochat tools logo