summaryrefslogtreecommitdiff
path: root/Chap5PracFantasyInv.py
blob: e8235f302773b476eda25e590ddd7dd360ed0e87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!Python

#Chapter 5 Fantasy Game Inventory Practice

inv = {'rope': 1, 'gold coin': 42}

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


def addToInventory(inventory, addItems):
    for k in range(len(addItems)):
        if addItems[k] in inventory.keys():
            inventory[addItems[k]] += 1
        else:
            inventory.setdefault(addItems[k], 1)
    return inventory


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

inv = addToInventory(inv, dragonLoot)
displayInventory(inv)