TypeError:不支持+的操作数类型:“int”和“str”如何修复

2024-04-26 18:27:29 发布

您现在位置:Python中文网/ 问答频道 /正文

a = 1
s = 0
print ('Enter Numbers to add to the sum')
print ('enter 0 to quit')
while a != 0:                  
        print ('Current Sum',s)
        a = input('Number?: ') 
        s = s + a
print ('Total sum: ',s)

如何修正这个错误?在


Tags: thetoaddnumberinputcurrentquittotal
2条回答

input()的返回类型是str。必须显式地将其类型转换为int类型。s += int(a)会很好地工作。在

你必须把输入的字符串转换成数字。例如:

    a = int(input('Number?: '))

其他新闻:

您可以使用short-cut+=运算符求和s

^{pr2}$

相关问题 更多 >