简体   繁体   English

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

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

I am new to Python and am learning some basics.我是 Python 的新手,正在学习一些基础知识。 I would like to know why I am getting this error.我想知道为什么我会收到这个错误。 The code is:代码是:

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)

The error I'm getting is:我得到的错误是:

`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'

Any and all help is appreciated!任何和所有的帮助表示赞赏!

You forgot to convert to type int in this line:您忘记在此行中转换为int类型:

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

Should be:应该:

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

I updated the code check now.我现在更新了代码检查。

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.

相关问题 类型错误:&lt;&lt;:&#39;str&#39; 和 &#39;int&#39; 的操作数类型不受支持 - TypeError: unsupported operand type(s) for <<: 'str' and 'int' TypeError:-:“ int”和“ str”不支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'int' and 'str' 类型错误:不支持 / 的操作数类型:&#39;str&#39; 和 &#39;int&#39; (2) - TypeError: unsupported operand type(s) for /: 'str' and 'int' (2) TypeError:-:“ str”和“ int”的不受支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'str' and 'int' TypeError:+ =不支持的操作数类型:“ int”和“ str” - TypeError: unsupported operand type(s) for +=: 'int' and 'str' typeerror 不支持 / &#39;str&#39; 和 &#39;int&#39; 的操作数类型 - typeerror unsupported operand type(s) for / 'str' and 'int' TypeError:-:“ str”和“ int”的不受支持的操作数类型 - TypeError:unsupported operand type(s) for -: 'str' and 'int' + 不支持的操作数类型:“int”和“str”[TypeError] - unsupported operand type(s) for +: 'int' and 'str' [TypeError] TypeError: 不支持的操作数类型 -: 'str' 和 'int' - TypeError: unsupported operand type(s) for -: 'str' and 'int' TypeError:|:“ int”和“ str”不支持的操作数类型 - TypeError: unsupported operand type(s) for |: 'int' and 'str'
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM