Anonymous Anonymous - 4 months ago
1466 0

Really basic inventory.

Python

Inventory

# A bsic model of inventory for game

playerInventory = {'arrow': 12,
    'gold coin' : 42,
    'rope' : 1,
    'torch' : 6,
    'dagger' : 1}

def displayInventory(inventory):
    print('Inventory:')
    totalItems = 0
    for i, j in inventory.items():
        print(j, i)
        totalItems += j

    print('Total number of items: ' + str(totalItems))

displayInventory(playerInventory)




dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']

def addToInventory(inventory, addedItems):
    for i in addedItems:
        if i in inventory:
            inventory[i] += 1
        else:
            inventory[i] = 1

addToInventory(playerInventory, dragonLoot)
displayInventory(playerInventory)