Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python (retired) Hack-n-Slash Warriors! Come Out and Play-ay!

Kevin Lankford
Kevin Lankford
1,983 Points

questions about creating the constructor to carry attack_limit and weapon

from character import Character

class Warrior(Character):

def __init__(self, weapon, attack_limit):
    self.weapon = 'sword'
    self.attack_limit = attack_limit

def rage(self):
    self.attack_limit = 20

This does not seem the be recognized as the correct answer, although I feel like I have everything set correctly for the object to initialize. Can someone point out what is causing the test to reject this solution?

warrior.py
from character import Character

class Warrior(Character):

    def __init__(self, weapon, attack_limit):
        self.weapon = 'sword'
        self.attack_limit = attack_limit

    def rage(self):
        self.attack_limit = 20

2 Answers

Ryan S
Ryan S
27,276 Points

Hi Kevin,

What part of the challenge are you on? I'm a little confused as how you made it to step 3 using the __init__ function because the challenge doesn't ever ask you to set it up. Step 4 asks you to set up the string conversion function, __str__, maybe you misunderstood what it was asking?

The first 3 steps should look like this

from character import Character

class Warrior(Character):
    weapon = 'sword'

    def rage(self):
        self.attack_limit = 20

Good luck.

Kevin Lankford
Kevin Lankford
1,983 Points

Yes, the first three parts I had no problem with. The fourth challenge was to have the instance be set up as "Warrior, <weapon>, <attack_limit>", that is why I thought to have an initializing constructor. However this does not seem to be the correct answer. Are you familiar with what may be the issue? Thanks

Ryan S
Ryan S
27,276 Points

Hi Kevin,

The 4th step in the challenge is asking you to produce a specific string when converting Warrior to a string.

For this you will need to set up the __str__ function, not __init__. You have already set up the variables and just need to return them in a string.

from character import Character

class Warrior(Character):
    weapon = 'sword'

    def rage(self):
        self.attack_limit = 20

    def __str__(self):
        return "Warrior, {}, {}".format(self.weapon, self.attack_limit)