【发布时间】:2016-03-17 18:03:36
【问题描述】:
这是一个随机数学问题,会返回一个我没想到的错误。请帮忙! 错误是: print('你的分数是:'+int(score)) TypeError: 无法将“int”对象隐式转换为 str
import random
count=0
while count<10:
numb1=random.randint(1,12)
numb2=random.randint(1,12)
ops=[' add ',' times ',' takeaway ']
ops2=random.choice(ops)
question=str(numb1)+''.join(ops2)+str(numb2)
print(question)
ans=int(input('Answer: '))
count=count+1
score=0
if ans== numb1+numb2 or numb1-numb2 or numb1*numb2:
score=score+1
print('Your score was: '+score)
【问题讨论】:
-
可能是因为 Python 不会将整数隐式转换为字符串?
-
你不能把 str 和它结合起来。 print('你的分数是:'+str(score))
-
print('Your score was: {}'.format(score)) -
谢谢!现在我已经输入了 str(score))