The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Is my teacher teaching us well?
Discuss Is my teacher teaching us well? in the Python Programming forum on Dev Shed. Is my teacher teaching us well? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 20th, 2012, 12:58 PM
|
|
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
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?
|

November 20th, 2012, 03:06 PM
|
 |
Contributing User
|
|
|
|
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.
|

November 23rd, 2012, 01:55 AM
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|