Skip to content

Instantly share code, notes, and snippets.

@toonarmycaptain
Created July 27, 2017 20:07
Show Gist options
  • Save toonarmycaptain/22d6f14681c4cf11dea2044df54a0861 to your computer and use it in GitHub Desktop.
Save toonarmycaptain/22d6f14681c4cf11dea2044df54a0861 to your computer and use it in GitHub Desktop.
Automate the Boring Stuff Chpt 5 - Fantasy Game Inventory
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)
@awalsh882
Copy link

When I run this code, I'm only getting 43 coins, rather than 45.

@toonarmycaptain
Copy link
Author

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?

@kosi53
Copy link

kosi53 commented Jul 4, 2020

`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)`

@drumwas
Copy link

drumwas commented Nov 16, 2020

When I run this code, I'm only getting 43 coins, rather than 45.

Try adjusting: inventory[item]=inventory[item] + 1.

@toonarmycaptain
Copy link
Author

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]

@BRSRKRed
Copy link

BRSRKRed commented Dec 25, 2020

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.

@Vilius28
Copy link

Vilius28 commented Feb 9, 2021

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.

@toonarmycaptain
Copy link
Author

@Villus28 you use spaces. :) 4 spaces is convention in python, and people prefer spaces to tabs.

@KoiDev13
Copy link

Thank you very much 🗡️ I'm having hard time to figure it out.

@toonarmycaptain
Copy link
Author

Thank you very much dagger I'm having hard time to figure it out.

image

@ImreKovats
Copy link

Thanks man, you are clever!

@iamjonh
Copy link

iamjonh commented Jan 26, 2022

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))`

@muning24
Copy link

thank you <3

@solmv
Copy link

solmv commented Apr 29, 2024

thank you!
I was having a hard time with the second part of the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment