TypeError: can only concatenate tuple (not "int") in Python
Asked Answered
T

6

10

I'm going to need your help with this constant tuple error I keep getting. Seems that it is a common mathematical error that many have. I have read almost every instance of TypeError including 'not int', 'not list', 'not float' etc. Yet I have failed to figure out why I get it.

I have written the below code that allows you to enter the sum of random number and in the end it calculates your success ratio. So I have a counter "right=right+1" to count my correct answers. Seems as if Python doesn't like that.

Here is what I have written:

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
    results(right, averageRight)
    displayInfo(studentName, right, averageRight)

def declareVariables():
    counter = 0
    studentName = 'NO NAME'
    averageRight = 0.0
    right = 0.0
    answer = 0.0
    number1 = 0
    number2 = 0
    return counter, studentName, averageRight, right, answer, number1, number2
    
def inputNames():
    studentName = raw_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 = input('What is the sum: ')
    return answer
    
def checkAnswer(number1, number2, answer, right):
    if answer == number1+number2:
        print 'Right'
        right = right + 1
    else:
        print 'Wrong'
        
    return right, answer
    
def results(right, averageRight):
    averageRight = right/10
    return averageRight
    
    

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

and I keep getting:

Traceback (most recent call last):
  File "Lab7-4.py", line 70, in <module>
    main()
  File "Lab7-4.py", line 15, in main
    right = checkAnswer(number1, number2, answer, right)
  File "Lab7-4.py", line 52, in checkAnswer
    right = right + 1
TypeError: can only concatenate tuple (not "int") to tuple Press any key to continue . . .
Taima answered 26/10, 2013 at 17:57 Comment(0)
L
17

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 it's 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 don't 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
Lukas answered 26/10, 2013 at 18:1 Comment(0)
P
2
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.

Preclinical answered 26/10, 2013 at 18:0 Comment(0)
V
1

I don't 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()
Verminous answered 7/12, 2016 at 4:11 Comment(0)
I
0

Try rightFloat = float(right[0] + 1) and just reference rightFloat. Just a workaround in case you get lazy.

Inextirpable answered 26/10, 2013 at 18:7 Comment(8)
Did you actually read the OP code? right is meant to be a number throughout, the fact that it becomes a tuple is a surprise, not the goal.Lukas
Then in that case take the first value of the tuple and add the one to it. Not hard.Inextirpable
and what about the first call where right is set to 0.0? Instead of fixing the symptom, fix the actual problem.Lukas
Point taken. But then again You could just do intRight = right[0] + 1 and then just use intRight wherever needed.Inextirpable
Sorry not int. use a floatInextirpable
Or a decimal if really necessary.Inextirpable
But the problem here is that one function returns a tuple because the OP misunderstood some syntax. Fixing that means there is no tuple at all and the problem goes away.Lukas
Tackling the problem is better than fixing sub-problems it creates. Agreed. Okay, disregard my answer please. Just a workaround if you get lazy.Inextirpable
V
0

I don´t understand were is the problem in this code but all time show "Exception has occurred: TypeError can only concatenate tuple (not "int") to tuple"

C=  (float(10))
F= (float((C * 1,8) + 32),1)
while C<=100:
 print(F)
 C += 10
Vernation answered 16/5, 2022 at 15:49 Comment(1)
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From ReviewWallboard
C
0

I got the same error below:

TypeError: can only concatenate tuple (not "int") to tuple

When trying to concatenate tuple type and int type as shown below:

    # tuple # int
print((1, 2) + 3)

So, I changed int type to tuple type as shown below:

print((1, 2) + (3,))

Then, the error above was solved:

(1, 2, 3)
Crews answered 23/12, 2022 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.