日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python好玩游戏的物品清单_Python 好玩游戏的物品清单

發布時間:2023/12/20 python 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python好玩游戏的物品清单_Python 好玩游戏的物品清单 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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)

運行結果:

Inventory:

1 rope

6 torch

42 gold coin

1 dagger

12 arrow

Total number of items: 62

列表到字典的函數,針對好玩游戲物品清單

假設征服一條龍的戰利品表示為這樣的字符串列表:

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

寫一個名為 addToInventory(inventory, addedItems)的函數, 其中 inventory 參數

是一個字典, 表示玩家的物品清單(像前面項目一樣), addedItems 參數是一個列表,

就像 dragonLoot。

addToInventory()函數應該返回一個字典, 表示更新過的物品清單。請注意, 列

表可以包含多個同樣的項。你的代碼看起來可能像這樣:

def addToInventory(inventory, addedItems):

# your code goes here

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

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

inv = addToInventory(inv, dragonLoot)

displayInventory(inv)

新代碼:

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)

def addToInventory(inventory, addedItems):

# your code goes here

for i in addedItems:

if i in inventory.keys():

inventory[i] += 1

else:

inventory.setdefault(i,1)

return inventory

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

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

inv = addToInventory(inv, dragonLoot)

displayInventory(inv)

運行結果:

Inventory:

45 gold coin

1 rope

1 dagger

1 ruby

Total number of items: 48

原文:https://blog.51cto.com/xxy12345/2425053

總結

以上是生活随笔為你收集整理的python好玩游戏的物品清单_Python 好玩游戏的物品清单的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。