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

New Free Tools on Dev Shed!

#1
November 20th, 2012, 01:58 PM
 calum64
Registered User

Join Date: Nov 2012
Posts: 1
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
#Integer choice

print("2:  Subtract the numbers")
print("3:  Multiply the numbers")
print("4:  Divide the numbers")
print("5:  Exit")
print("-----------------------------------------------")

try:
#choice must be a value 1 through 5
while (choice < 1 or choice > 5):
print("You MUST enter a valid value")
return choice
except ValueError:
print("Non numeric input ignored. Try again...")
except:
print("Input Error!!")
###########################################################################
#This function adds two numbers and returns the sum
#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
while (choice != 5):
if (choice ==1):
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

#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
November 20th, 2012, 04:06 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,187
Time spent in forums: 1 Month 3 Weeks 2 Days 13 h 13 m 26 sec
Reputation Power: 455
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 04:08 PM.

#3
November 23rd, 2012, 02:55 AM
 russ123
Registered User

Join Date: Nov 2012
Posts: 19
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.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Is my teacher teaching us well?

 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 Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

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