-
-
Save toonarmycaptain/22d6f14681c4cf11dea2044df54a0861 to your computer and use it in GitHub Desktop.
stuff = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12} | |
#def displayInventory(inventory): | |
# total_items = 0 | |
# for item in inventory: | |
# print(str(inventory[item])+' '+item) | |
# total_items += inventory[item] | |
# print("Total number of items: "+str(total_items)) | |
# This worked but I like the following better: | |
def displayInventory(inventory): | |
total_items = 0 | |
for item, quantity in inventory.items(): | |
print(str(quantity)+' '+item) | |
total_items += quantity | |
print("Total number of items: "+str(total_items)) | |
displayInventory(stuff) | |
def addToInventory(inventory, addedItems): | |
for item in addedItems: | |
inventory.setdefault(item, 0) #this adds a (defaulted to zero value) key to the inventory dict if it's not already there | |
inventory[item] += 1 #and this increases that value by one, each time that item appears in the loot list | |
return inventory | |
inv = {'gold coin': 42, 'rope': 1} | |
displayInventory(inv) | |
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby'] | |
inv = addToInventory(inv, dragonLoot) | |
displayInventory(inv) |
When I run this code, I'm only getting 43 coins, rather than 45.
I haven't touched this in awhile (I'd probably be using f-strings if I did), but when I copy it in to a repl.it instance and run it, I don't get 43 coins...anywhere in the output?
`stuff = {'rope': 1, 'torch': 6, 'gold chain': 42, 'dagger': 1, 'arrow': 12}
def displayInventory(inventory):
print('Inventory:')
item_total = 0
for k, v in inventory.items():
print(v, k)
item_total = item_total + v
print('Total number of items: ' + str(item_total))
displayInventory(stuff)`
When I run this code, I'm only getting 43 coins, rather than 45.
Try adjusting: inventory[item]=inventory[item] + 1.
When I run this code, I'm only getting 43 coins, rather than 45.
Try adjusting: inventory[item]=inventory[item] + 1.
That is what inventory[item] += 1
does, except you don't have to repeat (and potentially misspell) inventory[item]
When I run this code, I'm only getting 43 coins, rather than 45.
Try adjusting: inventory[item]=inventory[item] + 1.
That is what
inventory[item] += 1
does, except you don't have to repeat (and potentially misspell)inventory[item]
I had the same problem counting the coins, then I realized I nested return inventory
in the for loop. I placed it after the for loop and now it works fine.
Hi,
This is what I have with the latest Python version 3.9.1:
def addToInventory(item, number):
empty = {}
for z in number:
for i, k in item.items():
if i == z:
item[i] = k + 1
else:
empty[z] = 0
finalDict = empty | item
print(finalDict)
inv = {"gold coin": 42, "rope": 1}
dragonLoot = ["gold coin", "dagger", "gold coin", "gold coin", "ruby"]
addToInventory(inv, dragonLoot)
Edit: not sure how to tab it.
@Villus28 you use spaces. :) 4 spaces is convention in python, and people prefer spaces to tabs.
Thank you very much 🗡️ I'm having hard time to figure it out.
Thanks man, you are clever!
I just want to share my code on how I solve this exercise :)
`inventory = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
def addInventory(inventory, loots):
for loot in loots:
addedQty = 0
if loot in inventory.keys():
addedQty += 1
inventory[loot] += addedQty
else:
inventory.setdefault(loot, 1)
return inventory
def displayInventory(inventory):
total = 0
for key, value in inventory.items():
print(str(value) + ' ' + key)
total += value
print('Total number of items: ' + str(total))
displayInventory(addInventory(inventory, dragonLoot))`
thank you <3
thank you!
I was having a hard time with the second part of the code.
When I run this code, I'm only getting 43 coins, rather than 45.