I am 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 . . .
Method #1 : Using + operator This is the most Pythonic and recommended method to perform this particular task. In this, we add two tuples and return the concatenated tuple. No previous tuple is changed in this process.
The Python "TypeError: can only concatenate tuple (not "list") to tuple" occurs when we try to concatenate a tuple and a list. To solve the error, make sure the two values are of type list or type tuple before concatenating them.
Practical Data Science using Python When it is required to concatenate two string tuples, the 'zip' method and the generator expression can be used. The zip method takes iterables, aggregates them into a tuple, and returns it as the result. Generator is a simple way of creating iterators.
In Python, since tuple is immutable, you cannot update it, i.e., you cannot add, change, or remove items (elements) in tuple . tuple represents data that you don't need to update, so you should use list rather than tuple if you need to update it.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With