我的程序是一个冒险游戏药水和箱子。我将这些设置在列表中,因此当用户喝了一剂药水或打开一个箱子时,我可以使用pop()
将它从列表中删除。当用户在同一个房间里时,这可以正常工作,但是如果他们回到房间,他们可以再次使用该药水或胸部。列表和流行()不工作,因为我期望
伪代码,系统应工作是这样的:
if potions:
if there are still potions in the list:
drink potion
potions.pop()
else:
print "There are no potions left."
else:
print "There are no potions to drink."
我认为正在发生的事情是,当我从列表弹出药水,if potions
不计算到为真,它会自动进入到其他区块,但我不确定它为什么这样做,我也不确定为什么当我回到房间时,列表会自动重置。
这很可能是因为我没有完全理解列表或pop方法是如何工作的,所以如果有人能够澄清那会很好。 谢谢!
编辑真正的代码从OP的链接
if room.potions:
if len(room.potions) > 0: # if there are potions left
if hasattr(room.potions[0], 'explode'): # if the potion is a bomb
room.potions[0].explode(You) # explode using zeroeth item from the list
room.potions[0].pop # remove zeroeth item from the list`
else:
room.potions[0].heal(You) # heal using zeroeth potion from the list
room.potions.pop(0) # remove zeroeth item from the list
else:
print "There are no more potions to drink"
else:
print "There are no potions to drink"
编辑:解决
我的问题是,当我把在词典中的参数,我把整个房间作为一个他们,认为我能够使用它的一切都很好。但是,每次我这样做,它都会将空间设置为init,从而有效地破坏我正在尝试做的事情。我为魔药和胸部添加了单独的参数,现在它完美地工作。 谢谢@Bernie提供的有用建议可以让我看到错误。 也谢谢你@你的建议和提示。 ,
if potions: # same as len(potions) > 0
# drink potion
potions.pop()
else:
print "There are no potions to drink."
所以,检查是否有任何药水留如果是左图:
你可以发布代码给你一个错误吗? –
我会放一个魔药部分的链接。爆炸部分不是我所担心的。 [链接](https://www.dropbox.com/s/pecf9cyv2n45owt/testfile.py?dl=0) –
你也应该担心爆炸部分,因为它说药剂[0] .pop()而不是药剂。流行(0)。 –