类型错误:不受约束的方法打()必须与口袋妖怪实例作为第一个参数来调用(GOT型号实例,而不是)

问题描述:

这是我的代码:类型错误:不受约束的方法打()必须与口袋妖怪实例作为第一个参数来调用(GOT型号实例,而不是)

# pokemon1 
class Pokemon(object): 
    def __init__(self,name,hp,damage): 
     self.name = name  
     self.hp = hp   
     self.damage = damage 

    def fight(self,other): 
     if(self.hp > 0): 
      print("%s did %d damage to %s"%(self.name,self.damage,other.name)) 
      other.hp -= self.damage 
      if (other.hp > 0): 
       print("%s has %d hp left" % (other.name, other.hp)) 
      else: 
       print("%s has died" % (other.name)) 
      return other.fight(self) 
     else: 
      print("%s wins! (%d hp left)"%(other.name,other.hp)) 
      return other,self 

class pikachu(Pokemon): 
    def __init__(self): 
     super(pikachu, self).__init__('pikachu', 100, 10) 

class pidgy(Pokemon): 
    def __init__(self): 
     super(pidgy, self).__init__('pidgy', 200, 12) 

#main 
import pokemon1 

pikachu = pokemon1.pikachu 
pidgy = pokemon1.pidgy 

p = pokemon1.Pokemon 
p.fight(pikachu,pidgy) 

你需要调用口袋妖怪(),而不是宠物小精灵,在你倒数第二行来调用构造函数。

Reinderien是正确的,但我尝试了你的代码,发现了一个不同的问题,你设计了不同的类来尝试使用它。

此代码应工作,而不是:

pikachu = pokemon1.pikachu 
pidgy = pokemon1.pidgy 

pikachu.fight(pidgy)