2016-01-23 163 views
-2

我已經使用了這個錯誤,並且不明白它是什麼。我正在嘗試做一個簡單的遊戲,並且我不允許我稱之爲攻擊方法。Python class'int'object not callable

這是該行它說的錯誤是:

enemy.attack(player1) 
if player1.dead == True: 
    print "You have lost" 

這是方法:

def attack(self, player): 
    #defend = randint(0,10) 

    damage = randint(0, self.strength) 
    if damage == 0: 
     print "You missed" 
    elif damage < self.strength: 
     print "Hit for", damage,"damage." 
     player.health = player.health - damage 
    elif damage == self.strength: 
     print "Critical hit!" 
     player.health = player.health - (damage + 2) 
    player.check_dead(player) 

如果您需要了解代碼請詢問更多的信息。我不確定我打算給你什麼信息,因爲我不明白錯誤。

錯誤控制檯:

Your health: 100 
Attack or Heal attack 
Hit for 3 damage. 
97 

Traceback (most recent call last): 
    File "E:\Computing\player.py", line 119, in <module> 
    enemy.attack() 
TypeError: 'int' object is not callable 
+1

請發佈完整回溯(控制檯中的錯誤) –

+0

請將類別敵人和播放器發佈到源.. –

回答

6

在您指定的intenemy.attack一些點,然後試着撥打enemy.attack()的功能。

找一條線,如xyz.attack = {something},並仔細檢查{something}是什麼。


高級調試技術 - 使enemy.attack只讀屬性,它返回一個什麼都不做的功能,即

class enemy: 
    @property 
    def attack(self, player): 
     def null_fn(): 
      pass 
     return null_fn 

...現在運行你的代碼將拋出一個AttributeError指着其中一行您嘗試將值分配給attack ;-)

+0

我已經給它一個instanc e變量稱爲攻擊。 –