【发布时间】:2017-10-02 16:31:37
【问题描述】:
嗨,我刚开始编码,我正在练习一些东西,但我遇到了一些非常奇怪的东西。就是这样:
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
我没有在其他任何地方使用过'heal',所以它不能是重叠的名称(我检查过)。这很奇怪,因为我设计了 'heal' 和 'attack' 功能以完全相同的方式工作,但 'attack' 起作用而'heal' 不起作用。请帮忙
【问题讨论】:
-
您的帖子没有得到积极的关注,因为它难以阅读。请查看样式指南并编辑您的问题:stackoverflow.com/help/formatting
-
"我没有在其他任何地方使用过 'heal',所以它不能是名称重叠(我检查过)" - 下次按 Ctrl-F,因为您确实重用了名称,并且 Ctrl-F 立即找到重用.
标签: python python-3.x typeerror