分类: [python]

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

def addToInventory(inventory, addedItems): ...: # your code goes here ...: for i in range(len(addedItems)): ...: if addedItems[i] in inventory.keys(): ...: inventory[str(addedItems[i])]+=1 ...: else: ...: inventory[str(addedItems[i])]=1 ...: return inventory inv = {'gold coin': 42, 'rope': 1} ...: dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold c ...: oin', 'ruby'] ...: inv = addToInventor ......

4.10.2 字符图网格-python编程快速上手

grid = [['.', '.', '.', '.', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['O', 'O', 'O', 'O', 'O', '.'],
['.', 'O', 'O', 'O', 'O', 'O'],
['O', 'O', 'O', 'O', 'O', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
for i in range(len(grid[0])):
...: for j in range(len(grid)):
...: print(grid[j][i],end=' ')
...: print('\n')