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

Python

My addition program is continually saying my input answers are wrong. So, where did I go wrong, literally?

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()

8 Answers

The input function returns a string which means that in your checkAnswer function you are comparing a string and an int against each other, which means they will never be considered equal.

If you convert the return of input to an int like this:

def getAnswer(number1, number2, answer):
    print ("What is the answer to the following equation?: ")
    print (number1)
    print ("+")
    print (number2)
    answer = int(input("What is the sum?: ")) # Convert to int
    return answer

Then your comparison code will work.

Also while it won't produce an error I don't quite understand why your getAnswer function takes answer as a parameter. The passed in answer is never actually used in the function so you can remove that parameter without having any effect on your code, the same is true of the averageRight parameter of the results method.

You also have a lot of empty print statement spread throughout your code that serves no real purpose.

One other thing I'll say is that in Python it is rare to place spaces between function calls and the parenthesis belonging to it, like you are doing with your print calls. Also variable and function names are usually written in snake_case rather than camelCase. Formatting your code in a way that conforms with most other Python developers is a good habit to get into.

Thank you, that makes sense, regarding the int statement. So, to make sure I understand what you are saying about "The passed in answer..." as well as "the averageRight...results." Are you saying that where I am asking for a return of answer, I don't need that? And where I have the return of averageRight I don't need that, or I just don't need the def results, altogehter? I think that was suggested or required, in our assignments pseudocode, and so that is why I included it. I don't know why I put so many obsolete print statements in the code, at this moment. I've been struggling with this homework assignment for about a week. I have moved on to other labs to attempt to complete them and I keep revisiting this. I either saw them placed in the example video that comes with the assignment or maybe I found it in some else's example for a similar assignment, while scouring the internet trying to find what I've screwed up on. I didn't know that I was writing my functions in camelCase rather than snake_case. That difference hasn't been covered in our course material. Although, I have seen some examples wrote in snake_case, I couldn't have told you that it was called snake_case. Most of our examples in my class are wrote in camelCase, although again, I couldn't have told you that it was called camelCase, when it was wrote that way nor when I wrote it that way that it was called that; if that makes sense. We haven't covered that, nor the difference between the two. In my class, it is a lot of monkey see monkey do, without a great deal of explanations. The required book may have that information in there and I'll check but I kinda doubt it. It seems to be a lot of general information about various things to do with coding but nothing specific on why, why not or how. Thank you for your input, it has enlightened me on some things and others, it has brought up things that I need to check into and learn more about them.

Are you saying that where I am asking for a return of answer, I don't need that? And where I have the return of averageRight I don't need that, or I just don't need the def results, altogehter?

No, the returns are needed. What is not needed is the parameter. Basically instead of defining the function like this:

def getAnswer(number1, number2, answer):
    print ("What is the answer to the following equation?: ")
    print (number1)
    print ("+")
    print (number2)
    answer = int(input("What is the sum?: ")) # Convert to int
    return answer

You can define it like this:

def getAnswer(number1, number2): # answer removed as paramter
    print ("What is the answer to the following equation?: ")
    print (number1)
    print ("+")
    print (number2)
    answer = int(input("What is the sum?: ")) # Convert to int
    return answer

And then you can call the function like this:

answer = getAnswer(number1, number2)

Including answer in the parenthesis (which makes it a parameter) allows you to pass in a value to the function when it is called, but if you don't actually make any use of that value within the function then the parameter is pointless.

In the getAnswer function you create an answer variable and return the results of it, but that does not require you to pass answer in as a parameter.

Ok, I added in the int statement and I took out the nonessential print statements, now I have an error. I promise I didn't do anything else to it but my above code doesn't look like what I have... so the error statement says this: TypeError: getNumbers() missing 2 required positional arguments: 'number1' and 'number2' Oh, I see, I have two versions going. One version has the arguments and one didn't. However, even when I add the int to the one with the arguments, it is still coming up with the same error. So, fix one thing and on to the next thing.

import random

def main():
    #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

        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, number2):
    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 = int(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("Information for student: ", studentName)
    print("The number right is: ", right)
    print("The average right is: ", averageRight)


main()

In the code you posted before the getNumbers function looked like this:

def getNumbers():
    number1 = random.randint(1, 500)
    number2 = random.randint(1, 500)
    return number1, number2

In the code you post now it looks like this:

def getNumbers(number1, number2):
    number1 = random.randint(1, 500)
    number2 = random.randint(1, 500)
    return number1, number2

The difference is that the one in your new code has two parameters (number1 and number2) which means that it cannot be called without passing in two values. However the function doesn't really need to have any parameters so the function in my first code block is the right version.

Here is a version of your code with the right getNumbers code and some general cleanup:

import random


def main():
    # 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 removed as argument
        right = checkAnswer(number1, number2, answer, right)
        counter = counter + 1

        # end of while loop

        averageRight = results(right) # averageRight removed as argument
        displayInfo(right, averageRight, studentName)


# this function gets the players names
def getName():
    studentName = input("Enter students name: ")
    return studentName


def getNumbers():  # getNumbers with no parameters
    number1 = random.randint(1, 500)
    number2 = random.randint(1, 500)
    return number1, number2


# getAnswer
def getAnswer(number1, number2): # answer removed as parameter 
    print("What is the answer to the following equation?: ")
    print(number1)
    print("+")
    print(number2)
    answer = int(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 removed as parameter 
    averageRight = float(right) / 10
    return averageRight


# print average
def displayInfo(right, averageRight, studentName):
    print("Information for student: ", studentName)
    print("The number right is: ", right)
    print("The average right is: ", averageRight)


main()

Ok, so I went back to the pseudocode. I changed the:

number1, number2 = getNumbers()

to :

getNumbers(number1, number2)

Now, a new problem, instead of giving me random numbers for my addition problems, it's just giving me what is 0 + 0, every iteration...jeez, now what.

Oh, ok. I see what you are referring to, regarding the needless inclusion of answer, thank you. I will remove them. And try to figure out why all of my addition is 0 + 0 now. I hope that's the last thing to fix, with this one.

Ok, that explanation helps. See, when I get an error, I don't usually know exactly what I am supposed to be fixing, nor what I did wrong, obviously. So, I look for answers, on Google, etc because most of the explanations on the Python page, go over my head. They don't really look like the things we are doing in my class so, they just confuse me. So, I then just lok up examples and such and keep adding and changing things until I get it to work, not always understanding why in the end it worked or didn't.

Ok, so...after my explanation of most of the time I don't understand what I've done wrong or how or what to fix, when I get an error. And after your revision(clean up) with explanations, I reversed what I had changed, regarding the getNumbers(number1, number2). I changed it back to, number1, number2 = getNumbers(). So, now, everything, the math, the counter, a right answer is being counted as a right answer and a wrong answer as a wrong answer. The average worked out accurately, all is well. Although, I'm not sure that I understand why when I made the previous change that my math problems were all 0 + 0, rather than random numbers added to random numbers. About the time I think I understand something with Python, then I find that I don't or something new comes along that I don't understand. I really want to finish my Python classes on treehouse because then maybe I will actually understand Python better. Right now, I have so much homework still due, that I'm not able to work on my treehouse classes, unfortunately.

In your previous code you got 0 + 0 because you did not assign the result of the getNumbers function. If your code had been like this:

number1, number2 = getNumbers(number1, number2)

Then your code would have worked. But passing in those two numbers to the function doesn't actually do anything useful, which is why I just removed the need to pass in numbers entirely in the cleaned up version I posted.

I would indeed recommend that you at some point go through the courses in the Learn Python track. Some of the stuff in those courses will be things you already have some knowlege of (like math operations, creating functions, etc) but they also cover and explain a lot of stuff that you seem to be struggling with at the moment.

I see, I think... I'm very thankful for your patience, insight and instruction. I have started some of the python classes but haven't got very far yet. So far, they certainly have helped me understand some things better but I'm sure I still have a ways to go. Thank you again.