【发布时间】:2017-02-26 21:49:28
【问题描述】:
在我的游戏代码中,用户绕着一个网格移动,如果他们降落在一个宝箱上,他们的硬币收集量会增加 10。但是一旦你降落在同一个宝箱上 3 次,它就会变成一个强盗并设置你的硬币集合到 0。我对此感到很苦恼,因为当用户首先确定有多少值时,很难计算列表中的单个值。这是我目前的代码:
TreasureCoords[(x,y)] = 0
Visits = TreasureCoords.get((x,y), None)
if Visits is None:
pass # Nothing happens
elif Visits > 3:
print("Uh oh there's a bandit! Your coin collection is now 0.")
CoinCollection = 0
else:
print("You have landed on a treasure chest! You can visit it %i more times before it becomes a bandit." % (2 - visits))
CoinCollection += 10
TreasureCoords[(x,y)] += 1
但是,当实现到我的代码中时,会发生此错误: 宝藏坐标[(x,y)] = 0 TypeError:列表索引必须是整数,而不是元组 我相信这与上面的代码使用 TreasureCoords 作为字典有关,而事实上它是一个包含宝箱所在位置的 x 和 y 值的列表。我不相信我可以将我的 TreasureCoords 和 BanditCoords 更改为字典,因为值是随机设置的,值的数量是由用户设置的,因此我看不出你将如何设置键或任何东西。我该怎么办?
【问题讨论】:
标签: python list dictionary