Search code examples
pythonattributestypeerror

Whats up with this error: "Int object has no attribute "strengthPts"?


I'm creating some simple code for my Intro to Python class. However, I keep recieving an attribute error on line 12: "AttributeError: 'int' object has no attribute 'strengthPts'". Here is my code:

class superhero:
    def __init__(self, name = "", strengthPts = 0, motto = "", strength = ""):
        self.name = name
        self.strengthPts = int(strengthPts)
        self.motto = motto
        self.strenght = strength
    def addStrengthPts(self, points):
        self.strengthPts = self.strengthPts + points
def main():
    s = superhero("Señor Cement", 0, "These streets won\'t stop me!", "creating cement from matter around him and using it to halt villains.")
    superhero.addStrengthPts(0,5)
    print("Today on Super of the Week, we present to you" + s.name + "After taking down a villain," + s.name + "gained" + str(s.strengthPts) + "points! With their power of" + s.strength + "and the awesome motto,\"" + s.motto + " you\'ll definitely want to give their comic a read!")

main()

As you can see, I assigned the class function superhero to s in line 10, believing that the problem might have been that I was refrencing the class incorrectly. Additionally, I've tried reassigning strengthPts as an integer, believing python may not be reading the perameter as one. However, none of this has worked, and I just keep recieving the same error. Any ideas?


Solution

    • The main problem is that you use the class name instead of the instance variable. Use s when calling the method addStrengthPts to get further
    • Next: You specify 0,5 but that is a tuple with two integers. Not sure what is your intention when adding points to self.strengthPts
    • Final: you have a typo in fourth member variable (see __init__). I guess you meant self.strength

    Hope this helps