Python TypeError:必须使用Pokemon实例作为第一个参数调用未绑定的方法fight()(改为获取类型实例)

Python TypeError:必须使用Pokemon实例作为第一个参数调用未绑定的方法fight()(改为获取类型实例),python,Python,这是我的代码: # 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 di

这是我的代码:

# 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)

您需要在最后一行调用构造函数的第二行中调用Pokemon(),而不是Pokemon。

Reinderien是正确的,但我尝试了您的代码,发现了一个不同的问题,您设计的类与尝试使用它的方式不同

此代码应该可以工作:

pikachu = pokemon1.pikachu
pidgy = pokemon1.pidgy

pikachu.fight(pidgy)