Я новичок в изучении python с книгой Автоматизация скучных вещей с Python от Альберта Суигарта.
inventory = {'arrows': 12, 'gold coins': 42, 'rope': 1, 'torches': 6, 'dagger': 1}
Из этого словаря мне нужно сделать вывод, подобный этому :
Inventory:
12 arrows
42 gold coins
1 rope
6 torches
1 dagger
Total number of items: 62
Пока я делаю что-то вроде этого и пытаюсь использовать методы из книги:
inventory = {'arrows': 12, 'gold coins': 42, 'rope': 1, 'torches': 6, 'dagger': 1}
def displayInventory(inventory):
totalNum = 0
for k, v in inventory.items():
print(v, k)
totalNum = totalNum + sum(inventory.values())
print("Total items of inventory: ")
return totalNum
print("Your inventory: ")
print(displayInventory(inventory))
Вывод:
Your inventory:
12 arrows
42 gold coins
1 rope
6 torches
1 dagger
Total items of inventory:
310
Почему мой totalNum
такой большой?