2015-04-03 119 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