-1

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

gtux
  • 538
  • 3
  • 15
  • 1
    Your post isn't getting positive attention because it is difficult to read. Please have a look at the style guide and edit your question: https://stackoverflow.com/help/formatting – slightlynybbled Oct 02 '17 at 16:35
  • 2
    "I havent used 'heal' anywhere else so it cant be names overlaping(I checked)" - Ctrl-F next time, because you did reuse the name, and Ctrl-F finds the reuse immediately. – user2357112 Oct 02 '17 at 16:36

2 Answers2

1

In your __init__ method you define an instance variable heal, which is an int. This overwrites the method of the same name.

Use a different name for that attribute; it seems like you meant health anyway.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

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.

slightlynybbled
  • 2,408
  • 2
  • 20
  • 38