2016-11-04 28 views
1

所以我跑我的代碼時,收到以下錯誤信息:「類型錯誤:不支持的操作數類型(S)爲+:‘詮釋’和‘海峽’」創建基本的計算器時(總和只)

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

代碼本身是:

items = [] 

item = '' 


while item != 'done': 
    item = (input("Enter value of item or type 'done':")) 

    if item != 'done': 
     int(item) 
     items.append(item) 

print('The total of your items is', sum(items)) 

我在這個模塊中丟失或做錯了什麼?我是否過早/過晚轉換爲整數?在初始輸入過程中我無法將其轉換,因爲它不會讀取「完成」來完成計算。如何調整代碼來總計我的items變量沒有這個錯誤?

回答

0

的問題是這一行:

 int(item) 

你正確地轉換,但沒有使用的價值。

它改成這樣:

 item = int(item) 
+0

完美地工作! – maaier

相關問題