0

I am trying to add multiple integers from a class in python however it comes up with the error:

Traceback (most recent call last): File "G:/documents/Computing/Python/Fighter Game.py", line 53, in if player.health() + player.strength() + player.defence() + player.speed() == 350: TypeError: 'int' object is not callable

The code for the class is:

class Fighter:
    name = "Not Set"
    alignment = "Not Set" #good / bad
    health = 0
    strength = 0
    defence = 0
    speed = 0
    def name(self):
        return self.name
    def alignment(self):
        return self.alignment
    def health(self):
        return self.health
    def strength(self):
        return self.strength
    def defence(self):
        return self.defence
    def speed(self):
        return self.speed
    def set_name(self, new_name):
        self.name = new_name
    def set_alignment(self, new_alignment):
        self.alignment = new_alignment
    def set_health(self, new_health):
        self.health = new_health
    def set_strength(self, new_strength):
        self.strength = new_strength
    def set_defence(self, new_defence):
        self.defence = new_defence
    def set_speed(self, new_speed):
        self.speed = new_speed

and the code that throws up the error is:

player = Fighter()
while True:
    player.set_name(input("Enter your name: "))
    player.set_alignment("good")
    player.set_health(int(input("Enter your health: ")))
    player.set_strength(int(input("Enter your strength: ")))
    player.set_defence(int(input("Enter your defence: ")))
    player.set_speed(int(input("Enter your speed: ")))
    if player.health() + player.strength() + player.defence() + player.speed() == 350:
        print("Player setup complete.")
        break
    else:
        print("Numerical player values must all add up to 350.")

Any help that could be given would be appreciated! :-)

2
  • .health can't be both a property and a method. You're overwriting the method with a number when you set_health. Commented Jul 8, 2016 at 12:30
  • You are using defence both as an attribute and as a function (def defence(self). Rename the function or the attribute and it should work. Commented Jul 8, 2016 at 12:30

1 Answer 1

4

You have two things that are called self.health; an integer and a method. This doesn't make sense.

Instance variables are often prefixed with _, and set up in __init__(), at least this is the "old-school" simple way of doing it:

def __init__(self):
  self._health = 40

def health(self):
   return self._health

And so on. But do look into attributes and properties for more elegant ways of doing this.

Sign up to request clarification or add additional context in comments.

1 Comment

And really, since the setters and getters are doing absolutely nothing, you may as well use plain attributes...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.