Momchil Milkov Momchil Milkov - 1 year ago 131
Python Question

TypeError: 'int' object is not callable VERY STRANGE

Hi just started coding and i am practicing some things but i bumped into something really strange. This is it:

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


As you can see i just made a class to practice and after that i added two guys:

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


After that i tried the 'attack' function i created and everything went just fine:

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


After that i tried the 'heal' function i created but his popped up:

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


I havent used 'heal' anywhere else so it cant be names overlaping(I checked). This is very strange because i designed the 'heal' and 'attack' functions to work exactly the same way but 'attack' works and'heal' doesnt. PLEASE HELP

Answer Source

In __init__, you have declared self.heal as an attribute... an integer. Rename that variable to something besides your function name and you should be good.

Look under the self.damage=60 line.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download