簡體   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