Python 2.7 好玩游戏的物品清单
2017-05-09 16:13
417 查看
好玩游戏的物品清单
假设征服一条龙的战利品表示为这样的字符串列表:dragonLoot = [‘gold coin’, ‘dagger’, ‘gold coin’, ‘gold coin’ , ‘ruby’]
写一个名为 addToInventory(inventory, addedItems)的函数,其中inventory参数是一个字典,表示玩家的物品清单,addedItems参数是一个列表,就像dragonLoot.结果显示如下:
背包更新中: 45 gold coin 1 rope 1 dagger 1 ruby Total number of items:48
我的代码如下:
def addTolnventory(inventory, addedItems):
print ("背包更新包:")
item_total = 0
for something in addedItems:
if something in inventory:
inventory[something] += 1
else:
inventory.setdefault(something,0)
inventory[something] +=1
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']
print addTolnventory(inv, dragonLoot)
结果输出:
背包更新中: 45 gold coin 1 rope 1 dagger 1 ruby Total number of items:48None
[Finished in 0.0s]
个人微博:http://weibo.com/wjrtaojiang,各种优惠券走起!!!
相关文章推荐
- Python3:好玩游戏的物品清单 和 列表到字典的函数
- python程序-->列表到字典的函数,针对好玩游戏物品清单
- #python 列表到字典的函数,针对好玩游戏物品清单
- WPF编游戏系列 之九 物品清单再优化
- WPF编游戏系列 之三 物品清单
- WPF编游戏系列 之九 物品清单再优化
- VM中python2.7运行skier游戏,shell重启问题!!!!!!
- WPF编游戏系列 之三 物品清单
- Learn Python The Hard Way (python 2.7) ex45.py 你来制作一个游戏
- 经典好玩的休闲游戏大合集,免费玩
- 好玩的塔防游戏---模仿魔兽的造塔RPG
- Python游戏:PyGame(转自韩宪平的博客)
- 一个好玩的绘画游戏
- CoderZh首款Python联机对战游戏 - NancyTetris1.0倾情发布(一)
- 一切从游戏开始-完整的一个python to hack 实例
- SpbAirIslands超级好玩的模拟养成ppc游戏,很好玩,推荐
- 谋杀你的公交车时间 最好玩的游戏手机
- Python游戏:PyGame
- 今天来和大家一起分享一下好玩的游戏---美国农场主
- dweep_极度好玩智力游戏