TypeError: can only concatenate tuple (not int) in Python

TypeError: can only concatenate tuple (not int) in Python

Your checkAnswer() function returns a tuple:

def checkAnswer(number1, number2, answer, right):
    if answer == number1+number2:
        print Right
        right = right + 1
    else:
        print Wrong

    return right, answer

Here return right, answer returns a tuple of two values. Note that its the comma that makes that expression a tuple; parenthesis are optional in most contexts.

You assign this return value to right:

right = checkAnswer(number1, number2, answer, right)

making right a tuple here.

Then when you try to add 1 to it again, the error occurs. You dont change answer within the function, so there is no point in returning the value here; remove it from the return statement:

def checkAnswer(number1, number2, answer, right):
    if answer == number1+number2:
        print Right
        right = right + 1
    else:
        print Wrong

    return right
right = checkAnswer(number1, number2, answer, right)

You are assigning what is returned by checkAnswer. But you are returning a tuple from it.

return right, answer

So, after the first iteration right becomes a tuple. And when it reaches

right = right + 1

the second time, it fails to add an int to a tuple.

TypeError: can only concatenate tuple (not int) in Python

I dont think your averageRight gives the right result. So I fixed the code. I am using IDLE 3.5.2 so some syntax might seem little different (for example print()). So below is the code. You are welcome 🙂

import random 
#the main function
def main():
    counter, studentName, averageRight, right, answer, number1, number2 = declareVariables() 
    studentName = inputNames()

    while counter < 10:
        number1, number2 = getNumber()
        answer = getAnswer(number1, number2, answer)
        right = checkAnswer(number1, number2, answer, right)
        counter = counter + 1

    A=results(right, averageRight)
    displayInfo(studentName, right, A)

def declareVariables():
    counter = 0
    studentName = NO NAME
    averageRight = 0.0
    right = 0
    answer = 0
    number1 = 0
    number2 = 0
    return counter, studentName, averageRight, right, answer, number1, number2

def inputNames():
    studentName = input(Enter Student Name: )
    return studentName

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

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: )) #input would be a int. without adding the int it would make answer a string instead of int. which was reason why it was giving wrong 
    return answer

def checkAnswer(number1, number2, answer, right):
    if answer==number1+number2:
        print (Right)
        right = right + 1
    else:
        print (Wrong)

    return right

def results(right, averageRight):
    averageRight = right/10
    return averageRight



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

# calls main
main()

Leave a Reply

Your email address will not be published. Required fields are marked *