from random import randrange lootTypes = ['armor', 'gold'] loot = { 'armor': { 'chest': [ { 'name': 'weaksauce plate of frailty', 'armorpoints': 5 }, 'another dict of chest armor type' ], 'legs': [ { 'name': 'weaksauce pants of bedwetting', 'armorpoints': 2 }, 'another dict of leg armor type', ], # etc }, } inventory = { 'worn': { 'chest': { 'name': '', 'armorpoints': 5 }, 'legs': { 'name': '', 'armorpoints': 10, }, # etc }, 'bags': { 'items': [] # List holding dictionaries of items }, 'gold': 100 # Whatever else type of stuff in inventory } def chest_loot(chest): # Assumes chest is a dictionary containing possible items # chest = {'gold': 10, 'armor': loot} armorTypes = ['chest', 'legs'] randint = randrange(len(lootTypes)) randLootType = lootTypes[randint] if randLootType == 'armor': randArmorType = armorTypes[randint] looted = loot[randLootType][randArmorType] inventory['bags']['items'].append(looted) print "You looted %s from the chest. It is now in your bags." % looted['name'] else: # If not armor, must be gold. inventory['gold'] += chest['gold'] print "You received %i gold pieces!" % chest['gold'] print "You now have a total of %i gold." % inventory['gold'] return
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