stuff = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12} def displayInventory(inventory): print("Inventory:") item_total = 0 for k, v in inventory.items(): print(str(v) + ' ' + k) item_total += v print("Total number of items: " + str(item_total)) displayInventory(stuff) # for is looping through the actual items in the addItems list. # It is not looping through the index...would need range() and len(). def addToInventory(inventory, addItems): itemCount = 1 # keeps track of new item entered into dictionary for item in addItems: if item in inventory: inventory[item] += 1 else: inventory.setdefault(item, 1) return inventory inv = {'gold coin': 42, 'rope': 1} dragonLoot = ['gold coin', 'dagger', 'dagger', 'gold coin', 'gold coin', 'ruby', 'dagger'] inv = addToInventory(inv, dragonLoot) displayInventory(inv)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question