TypeError:只能在 Python 中连接元组(不是“int”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19609991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: can only concatenate tuple (not "int") in Python
提问by sammojohn
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.
我需要你的帮助来解决这个不断出现的元组错误。似乎这是许多人常见的数学错误。我已经阅读了几乎所有 TypeError 实例,包括“not int”、“not list”、“not float”等。但我一直没有弄清楚为什么我会得到它。
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.
我编写了以下代码,允许您输入随机数的总和,并最终计算您的成功率。所以我有一个计数器“right=right+1”来计算我的正确答案。似乎 Python 不喜欢那样。
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 . . .
采纳答案by Martijn Pieters
Your checkAnswer()
function returns a tuple:
您的checkAnswer()
函数返回一个元组:
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.
这里return right, answer
返回一个包含两个值的元组。请注意,是逗号使该表达式成为元组;在大多数情况下,括号是可选的。
You assign this return value to right
:
您将此返回值分配给right
:
right = checkAnswer(number1, number2, answer, right)
making right
a tuple here.
right
在这里制作一个元组。
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:
然后当您再次尝试添加1
时,就会出现错误。你不会answer
在函数内改变,所以在这里返回值没有意义;从return
语句中删除它:
def checkAnswer(number1, number2, answer, right):
if answer == number1+number2:
print 'Right'
right = right + 1
else:
print 'Wrong'
return right
回答by thefourtheye
right = checkAnswer(number1, number2, answer, right)
You are assigning what is returned by checkAnswer
. But you are returning a tuple from it.
您正在分配checkAnswer
. 但是您正在从中返回一个元组。
return right, answer
So, after the first iteration right
becomes a tuple. And when it reaches
所以,在第一次迭代之后right
就变成了一个元组。当它达到
right = right + 1
the second time, it fails to add an int to a tuple.
第二次,它无法将 int 添加到元组中。
回答by DevinLynch99
Try rightFloat = float(right[0] + 1)
and just reference rightFloat
. Just a workaround in case you get lazy.
尝试rightFloat = float(right[0] + 1)
并参考rightFloat
. 只是一个解决方法,以防你变懒。
回答by Mahmud Hasan
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 :)
我不认为你averageRight
给出了正确的结果。所以我修复了代码。我使用的是 IDLE 3.5.2,所以有些语法可能看起来没什么不同(例如print()
)。所以下面是代码。不客气 :)
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()