[英]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.