尝试将列表添加到词典并计数。
FileTextBox.SelectionStart = FileTextBox.Text.Length;
FileTextBox.SelectionLength = 0;
始终获取错误“ TypeError:必须为str,而不是int inline:
inventory = {"rope": '2', "torch": '4', "sword": '1', "bow": '1', "arrow": '10'}
dragon_loot = ['dagger', 'gold coin', 'gold coin', 'arrow', 'rope']
def inventory_hero(inv):
print("item | quantity")
print("----------")
total_quantity = 0
for k, v in inv.items():
print(str(k) + " : " + str(v))
total_quantity += int(v)
print("----------")
print("total: " + str(total_quantity))
def add_to_inventory(inv, loot_from):
for i in loot_from:
inv.setdefault(i, 0)
inv[i] += 1
inventory_hero(inventory)
add_to_inventory(inventory, dragon_loot)
inventory_hero(inventory)
尝试计算字符串中的字符,带有+ =的所有内容都可以正常工作,但字典始终出错。