我真的被卡住了,我正在閱讀Python - 如何自動化無聊的東西,我正在做一個實踐項目。Python:類型錯誤不支持的操作數類型爲+:'int'和'str'
爲什麼會標記錯誤?我知道這與item_total有關。
import sys
stuff = {'Arrows':'12',
'Gold Coins':'42',
'Rope':'1',
'Torches':'6',
'Dagger':'1', }
def displayInventory(inventory):
print("Inventory:")
item_total = sum(stuff.values())
for k, v in inventory.items():
print(v + ' ' + k)
a = sum(stuff.values())
print("Total number of items: " + item_total)
displayInventory(stuff)
錯誤,我得到:
Traceback (most recent call last): File "C:/Users/Lewis/Dropbox/Python/Function displayInventory p120 v2.py", line 17, in displayInventory(stuff) File "C:/Users/Lewis/Dropbox/Python/Function displayInventory p120 v2.py", line 11, in displayInventory item_total = int(sum(stuff.values())) TypeError: unsupported operand type(s) for +: 'int' and 'str'
你回溯做,你貼的代碼實際上並不匹配。不是說'int()'調用很重要。 –