2015-04-03 87 views
0

我正在制作一个基于python 2.7文本的RPG,而我的战斗功能运行不正常。这里是我的代码:函数运行不正确

def attack(self): 
    print "A %r appears! It wants to fight!" % (self.name) 
    while (player.health > 0) or (self.health > 0): 
     player.health = player.health - ((randint(0,5)) + attack_multiplier(self.level)) 
     print "%r strikes! Your health is down to %r" %(self.name, player.health) 
     try: 
      player.weapon = (raw_input("What do you attack with? >>").lower) 
      if (player.inventory.get(player.weapon) > 0) and (player.health > 0) and (self.health > 0): 
       if weapon_probability() == "critical hit": 
        self.health = self.health - (((randint(0,5))) + (attack_multiplier(weapon_levels.get(player.weapon))) * 2) 
        print "Critical Hit!" 
       elif weapon_probability() == "hit": 
        self.health = self.health - ((((randint(0,5))) + (attack_multiplier(weapon_levels.get(player.weapon))))) 
        print "Hit!" 
       elif weapon_probability() == "miss": 
        self.health = self.health 
        print "Miss" 
       print "Enemy health down to %r!" % (self.health) 
      elif player.health <= 0: 
       print "Your health...it’s falling" 
       break 
      elif self.health <= 0: 
       print "Enemy vanquished!" 
       break 
     except ValueError: 
      print "You don't have that" 

我看到的是:

'Bat' strikes! Your health is down to 95 
What do you attack with? >>sword 
'Bat' strikes! Your health is down to 91 
What do you attack with? >>sword 
'Bat' strikes! Your health is down to 87 
What do you attack with? >>sword 
'Bat' strikes! Your health is down to 85 
What do you attack with? >>sword 
'Bat' strikes! Your health is down to 82 
What do you attack with? >> 

这只是不断重复和player.health甚至不断下降到底片。我找不到错误。这个函数是一个类的方法,并且player是另一个类的一个实例。

回答

0

您存储方法,而不是小写的输入字符串:

player.weapon = (raw_input("What do you attack with? >>").lower) 

因为你实际上并没有呼叫str.lower()那里。您可能没有将str.lower方法存储在player.inventory中,因此player.inventory.get(player.weapon)会改为返回None

因为在Python 2几乎什么都可以相对于其他对象,测试下令:

player.inventory.get(player.weapon) > 0 

然后总是False

调用方法应该可以解决至少这个问题:

player.weapon = raw_input("What do you attack with? >>").lower() 

而不是使用player.inventory.get()(返回默认和口罩的问题,使用player.inventory[player.weapon]这会抛出一个KeyError表明该用户没有按“T有武器,所以调整自己的异常处理程序来捕捉它:

except KeyError: 
    print "You don't have that" 
+0

OMG我怎么没有看到这一点。我很抱歉 – frg100 2015-04-03 17:39:23

+0

对不起,接受这么长的时间来接受答案...它的第一个问题 – frg100 2015-04-04 19:06:08

+0

@ frg100:不是问题!有些人已经等待了*年*,标记职位被接受,这一天几乎没有*长*。 :-) – 2015-04-05 15:33:08

-2
while (player.health > 0) or (self.health > 0): 

也许,你需要更换这个FRA与gment:

while (player.health > 0) and (self.health > 0): 
+0

作者写 “这只是不断重复和player.health甚至继续下降到底片”,我建议在这个片段的问题。 – kvorobiev 2015-04-03 17:41:47

0

在这一行:

player.weapon = (raw_input("What do you attack with? >>").lower) 

它应该是:

player.weapon = (raw_input("What do you attack with? >>").lower()) 

否则你存储的功能,而不是结果。 Python的对待每件事情都作为对象

您还可能有一个问题就在这里:

while (player.health > 0) or (self.health > 0): 

也许它可能是:

while (player.health > 0) *and (self.health > 0): 
+0

这是在player.weapon行。我需要一个或另一个,因为我想循环运行,直到我或我的敌人被击败 – frg100 2015-04-04 19:01:07