繁体   English   中英

类型错误:-= 不支持的操作数类型:'int' 和 'str'

[英]TypeError: unsupported operand type(s) for -=: 'int' and 'str'

我是 Python 的新手,正在学习一些基础知识。 我想知道为什么我会收到这个错误。 代码是:

a = 0
x = int(input('What is your first number? '))
y = int(input('What is your second number? '))
MATH = [x, y]
R = int(input('How many numbers do you want to use? '))
if R > 2:
    R -= 1
    New = input('What is the next number? ')
    MATH.append(New)
counter = 0
answer = MATH[counter]
for i in MATH:
  counter += 1
  MATH[counter]
  answer += answer
print(answer)

我得到的错误是:

`TypeError                                 Traceback (most recent call last)
<ipython-input-12-ec570ecd992a> in <module>()
     12 for i in MATH:
     13   counter += 1
---> 14   answer += MATH[counter]
     15 print(answer)
     16 

TypeError: unsupported operand type(s) for +=: 'int' and 'str'

任何和所有的帮助表示赞赏!

您忘记在此行中转换为int类型:

New = input('What is the next number? ')

应该:

New = int(input('What is the next number? '))

我现在更新了代码检查。

x = int(input('What is your first number? '))
y = int(input('What is your second number? '))
MATH = [x, y]
R = int(input('How many numbers do you want to use? '))
if R > 2:
    R -= 1
    New = int(input('What is the next number? ')) #fixed line
    MATH.append(New)
counter = 0
answer = MATH[counter]
for i in MATH:
  counter += 1
  MATH[counter]
  answer += answer

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM