类型错误:"int"对象不可调用 非常奇怪



嗨,嗨,我刚开始编码,我正在练习一些事情,但我碰到了一些非常奇怪的东西。就是这样:

class Hero:
def __init__(self,name,weapon):
    self.name=name
    self.health=100
    self.weapon=weapon
    if(weapon=='Sword'):
        self.damage=50
    elif(weapon=='Spear'):
        self.damage=40
    elif(weapon=='Bow'):
        self.damage=40
    elif(weapon=='Staff'):
        self.damage=60
    self.heal=20
def attack(self,a):
    a.health-=self.damage
    print(self.name,'attacked',a.name,'with a',self.weapon,'and dealt',self.damage,'damage')
    if(a.health>0):
        print(a.name,'has',a.health,'left')
    else:
        print(self.name,'killed',a.name)
def heal(self,a):
    a.health+=self.heal
    print(self.name,'healed',a.name,'with a',self.weapon,'and restored',self.heal,'health')
    print(a.name,'has',a.health,'left')

您可以看到,我刚刚上课了,然后我添加了两个人:

Bob=Hero('Bob','Spear')
Gonzo=Hero('Gonzo','Sword')

之后,我尝试了我创建的"攻击"功能,一切都很好:

>>> Bob.attack(Gonzo)
 'Bob attacked Gonzo with a Spear and dealt 40 damage
Gonzo has 60 left'

之后,我尝试了我创建的"治疗"功能,但他弹出了:

>>> Bob.heal(Gonzo)
Traceback (most recent call last):
  File "<pyshell#370>", line 1, in <module>
    Bob.heal(Gonzo)
TypeError: 'int' object is not callable

我在其他任何地方都没有使用过"治愈",因此它不能使名称重叠(我检查过)。这很奇怪,因为我设计了"治愈"one_answers"攻击"功能以完全相同的方式工作,但"攻击"工作和" heal"却没有。请帮助

__init__中,您已将self.heal声明为属性... integer。将该变量重命名为您的功能名称以外的某些内容,您应该很好。

看在self.damage=60线下。

在您的__init__方法中,您定义了一个实例变量heal,即INT。这覆盖了同名的方法。

为该属性使用其他名称;似乎无论如何您的意思是health

最新更新