Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 20th, 2012, 12:58 PM
calum64 calum64 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 calum64 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 9 sec
Reputation 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?

Reply With Quote
  #2  
Old November 20th, 2012, 03:06 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,460 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 56 m 42 sec
Reputation Power: 403
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.
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : November 20th, 2012 at 03:08 PM.

Reply With Quote
  #3  
Old November 23rd, 2012, 01:55 AM
russ123 russ123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 19 russ123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 21 m 37 sec
Reputation 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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Is my teacher teaching us well?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap