我将需要你的帮助,我不断得到这个不断的元组错误。似乎这是许多人常见的数学错误。我几乎读过TypeError的每个实例,包括'not int','not list','not float'等。但是我没弄明白为什么我得到它。
我编写了以下代码,允许您输入随机数的总和,最后计算您的成功率。所以我有一个计数器“right = right + 1”来计算我的正确答案。好像Python不喜欢那样。
这是我写的:
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()
我一直在接受:
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 . . .
答案 0 :(得分:13)
您的checkAnswer()
函数返回元组:
def checkAnswer(number1, number2, answer, right):
if answer == number1+number2:
print 'Right'
right = right + 1
else:
print 'Wrong'
return right, answer
此处return right, answer
返回两个值的元组。请注意,它是逗号,使该表达式成为元组;在大多数情况下,括号是可选的。
您将此返回值指定给right
:
right = checkAnswer(number1, number2, answer, right)
在这里制作right
元组。
然后,当您尝试再次向其添加1
时,会发生错误。你不要在函数中改变answer
,所以在这里返回值没有意义;将其从return
语句中删除:
def checkAnswer(number1, number2, answer, right):
if answer == number1+number2:
print 'Right'
right = right + 1
else:
print 'Wrong'
return right
答案 1 :(得分:1)
right = checkAnswer(number1, number2, answer, right)
您正在分配checkAnswer
返回的内容。但是你要从中返回一个元组。
return right, answer
因此,在第一次迭代后right
成为一个元组。当它达到
right = right + 1
第二次,它无法将int添加到元组中。
答案 2 :(得分:0)
我认为您的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()
答案 3 :(得分:-1)
试试rightFloat = float(right[0] + 1)
,然后引用rightFloat
。只是一个解决方法,以防你变得懒惰。