Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Cheri Castro
226 PointsIn my math test, I am continually given wrong results even when I answer the equations correctly.
This time I have checked my return statement and it seems it is in the only spot that Python will allow. I think the error lies in the getAnswer or checkAnswer sections. I could be totally wrong though. Any suggestions, anyone?
import random
def main():
print
#declare variables
counter = 0
studentName = "NO NAME"
averageRight = 0.0
right = 0.0
number1 = 0
number2 = 0
answer = 0.0
studentName = getName()
while counter < 10:
#initialize variables
number1, number2 = getNumbers()
answer = getAnswer(number1, number2, answer)
right = checkAnswer(number1, number2, answer, right)
counter = counter + 1
#end of while loop
print
averageRight = results(right, averageRight)
displayInfo(right, averageRight, studentName)
#this function gets the players names
def getName():
studentName = input("Enter students name: ")
return studentName
def getNumbers():
number1 = random.randint(1, 500)
number2 = random.randint(1, 500)
return number1, number2
#getAnswer
def getAnswer(number1, number2, answer):
print ("What is the answer to the following equation?: ")
print (number1)
print ("+")
print (number2)
answer = input("What is the sum?: ")
return answer
#checkAnswer
def checkAnswer(number1, number2, answer, right):
if answer == number1 + number2:
print ("Right")
right = right + 1
else:
print ("Wrong")
return right
#function getAverage
def results(right, averageRight):
averageRight = float(right)/10
return averageRight
#print average
def displayInfo(right, averageRight, studentName):
print
print("Information for student: ", studentName)
print("The number right is: ", right)
print("The average right is: ", averageRight)
main()