2016-08-04 66 views
0

我的程序是一个冒险游戏药水和箱子。我将这些设置在列表中,因此当用户喝了一剂药水或打开一个箱子时,我可以使用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." 

所以,检查是否有任何药水留如果是左图:

+4

你可以发布代码给你一个错误吗? –

+0

我会放一个魔药部分的链接。爆炸部分不是我所担心的。 [链接](https://www.dropbox.com/s/pecf9cyv2n45owt/testfile.py?dl=0) –

+0

你也应该担心爆炸部分,因为它说药剂[0] .pop()而不是药剂。流行(0)。 –

回答

1

你可以做的,当他们喝它做到这一点

potions = 1 
    if potions > 0: 
     drink potion 
     potions = potions - 1 
    else: 
     print "There are no potions left." 
+0

第二个是什么? –

+0

错字我从脚本中拷贝了脚本,现在修复它。 – thesonyman101

+0

如果有多个魔药会怎么样? –

0

你可以简单地编写代码如下喝它,从列表中删除,否则显示没有药水。

+0

这有帮助。我想我只是试图过度设计这个问题。唯一需要弄清楚的是为什么它不断重置列表。该列表是在房间的__init__函数中创建的。我建立它的方式不应该继续创建列表,它应该只做一次,对吧? –

+0

@BACeradsky我认为你很可能遇到了这个问题:http://docs.python-guide.org/en/latest/writing/gotchas/ – bernie

+0

@Bernie谢谢你的文档,但我想我找到了问题。我的程序相当大,所以我一直试图将某些东西分成模块并导入它们。主要的是各种词典,处理用户输入的东西。问题是它只返回一个值,即用户想要去的下一个房间的数量。所以也许因为它只返回那个值,它忘记了我所做的所有弹出? –

0

EAFP方法:

try: 
    potions.pop() 
except IndexError: 
    print "No potion left" 

更容易请求原谅比许可。这种常见的Python编码风格假定存在有效的键或属性,并且如果假设证明为假,则捕获异常。这种干净而快速的风格的特点是存在很多尝试和除了 陈述。该技术与常见的许多 其他语言LBYL风格如C.

0

OK对比。我被诱惑...... :)当然,应该存在一个药水层次,并且这个方法可能会成为'世界'的信息。等等......

import random 

class Potion: 
    """Define the Potion class. Could be explosive and provoke a damage""" 
    def __init__(self, explosive=None, damage=None): 
     if explosive == None: 
      self.__explosive = bool(random.getrandbits(1)) 
     else: 
      self.__explosive = explosive 

     if self.__explosive: 
      if damage == None: 
       self.__damage = random.randint(1,5) 
      else: 
       self.__damage = damage 
     else: 
      self.__damage = 0 

    def isExplosive(self): 
     return self.__explosive 

    def explode(self): 
     if self.isExplosive(): 
      return self.__damage 
     else: 
      return 0 

    def __str__(self): 
     if self.isExplosive(): 
      res = "explosive potion" 
     else: 
      res = "simple potion" 
     return res 

class Hero: 
    """Simple Hero class""" 
    def __init__(self, name): 
     self.__name = name 
     self.__hp = 100 
     self.__potions = [] 

    def receiveDamage(self, damage): 
     self.__hp = self.__hp - damage 
     if self.__hp <= 0: 
      print("I'm dead!") 

    def isAlive(self): 
     return self.__hp > 0 

    def getPotion(self, room): 
     """ Get the first potion in the room.If the potion 
      is explosive, explode and receive some damage 
      TODO: Trapped potions? 
     """ 
     potion = room.getFirstPotion() 
     print("%s take a %s" % (self.__name, potion)) 
     if potion.isExplosive(): 
      damage = potion.explode() 
      print("%s received %d hp of damage!!" % (self.__name, damage)) 
      self.receiveDamage(damage) 
     else: 
      self.__potions.append(potion) 

    def __str__(self): 
     res = "%s have %d HP and %d potions." % (self.__name, self.__hp, len(self.__potions)) 
     return res 

class Room: 
    def __init__(self, potions): 
     """Create a room with some potions""" 
     self.__potions = potions 

    def getFirstPotion(self): 
     """Return the first potion """ 
     if self.__potions: 
      potion = self.__potions.pop() 
     else: 
      potion = None 
     return potion 

    def havePotions(self): 
     return len(self.__potions) > 0 

    @property 
    def potions(self): 
     return self.__potions 

    def __str__(self): 
     return "This room have %s potions" % len(self.__potions) 

peter = Hero('Peter') 
room = Room(potions=[Potion() for i in range(5)]) 
print(room) 
print(peter) 

while room.havePotions(): 
    print("___________") 
    peter.getPotion(room) 
    print(room) 
    print(peter) 

输出:

这个房间有5个药水
Peter有100 HP和0药水。
___________
彼得拿一个简单的药水
这个房间有4个药水
彼得有100个HP和1个药水。
___________
彼得拿起炸药
彼得得到3点伤害!!
这个房间有3个药水
Peter有97 HP和1 药水。
___________
彼得拿起炸药
彼得得到3点伤害!!
这个房间有2个药水
Peter有94 HP和1 魔药。
___________
彼得拿着一个爆炸药水
彼得得到5马力的伤害!
这个房间有1个药水
Peter有89 HP和1 魔药。
___________
彼得拿起炸药
彼得得到1点伤害!!
这个房间有0药水
彼得有88 HP和1 药水。

相关问题