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