Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型错误:';int';对象不可调用非常奇怪_Python_Python 3.x_Typeerror - Fatal编程技术网

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

Python 类型错误:';int';对象不可调用非常奇怪,python,python-3.x,typeerror,Python,Python 3.x,Typeerror,嗨,我刚开始编写代码,我正在练习一些东西,但我遇到了一些非常奇怪的事情。就是这样: 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

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

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
Bob.heal(Gonzo) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 鲍勃·赫勒(冈佐) TypeError:“int”对象不可调用
我没有在其他地方使用过“heal”,所以它不能是名称重叠(我检查过)。这很奇怪,因为我设计的“治疗”和“攻击”功能的工作方式完全相同,但“攻击”有效,“治疗”无效。请帮助

\uuuu init\uuuu
方法中定义实例变量
heal
,该变量为int。这将覆盖同名方法


为该属性使用不同的名称;看起来你的意思是健康。

\uuuuu init\uuuuu
中,你已经声明了
self.heal
作为属性。。。一个
整数
。将该变量重命名为函数名之外的其他名称,您应该很好


查看
self.damage=60
行下的内容。

您的帖子没有得到积极的关注,因为它很难阅读。请看一下样式指南并编辑您的问题:“我没有在其他任何地方使用过‘heal’,因此它不能是名称重叠(我已选中)”-Ctrl-F,因为您确实重复使用了名称,Ctrl-F会立即找到重复使用的名称。谢谢,我想我是盲人或弱智。我们只知道,因为我们自己已经重复使用了100次。这可能不是最后一次了!快乐编码!