《Python编程快速上手》5.6 实践项目

5.6.1 好玩游戏的物品清单

1
2
3
4
5
6
7
8
9
10
11
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)
输出
1
2
3
4
5
6
7
Inventory:
1 rope
6 torch
42 gold coin
1 dagger
12 arrow
Total number of items: 62

5.6.2 列表到字典的函数,针对好玩游戏物品清单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def addToInventory(inventory, addedItems):
for i in addedItems:
if i in inventory:
inventory[i] += 1
else:
inventory[i] = 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 = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
inv = addToInventory(inv, dragonLoot)
displayInventory(inv)
输出
1
2
3
4
5
6
Inventory:
45 gold coin
1 rope
1 dagger
1 ruby
Total number of items: 48