-
-
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) |
The above code is working fine but
in the function call section,displayInventory(inv) is appearing two times should be one.
I don't remember doing this, but I probably wanted to see the inventory after adding each set of items?
Thanks for this. I was doing this for add to Inventory.
for item in addedItems:
if item in inventory:
inventory[item] += 1
else:
inventory[item] = 1
Afterwards I remembered earlier in the chapter there was a good shortcut specifically for this type of check.
for item in addedItems:
inventory.setdefault(item, 0)
inventory[item] += 1
The above code is working fine but
in the function call section, displayInventory(inv) is appearing two times should be one.
It will print the values of
variable inv to 43
When I run this code, I'm only getting 43 coins, rather than 45.
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.
THANK YOU! I was having such a hard time figuring this project out and a lot of the answers online don't really follow the chapters. This was perfect.