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!

Małgorzata Staniszewska
Małgorzata Staniszewska
2,431 Points

Why doesn't it work?

?

warrior.py
from character import Character
class Warrior(Character):
    weapon = "sword"
    def rage(self):
        self.attack_limit = 20

    print("Warrior, {}, {}".format(self.weapon, self.attack_limit)

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! The string itself that you're printing would be correct. But the challenge isn't asking you to print this string. It's asking you to define what happens when we convert the object to a string. This requires a special method __str__.Take a look:

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

This method will print out the object in a nice, neat format.

edited to include documentation

You can find documentation on this method here

Hope this helps! :sparkles:

Małgorzata Staniszewska
Małgorzata Staniszewska
2,431 Points

I've done like u said: 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.attact_limit)

It doesn't work either.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Take a look at the .format() method. You've misspelled attack_limit. Also, I'm not sure if it's because your code is not in markdown, but I can't see the underscores around the "str". def __str__(self): :sparkles:

Małgorzata Staniszewska
Małgorzata Staniszewska
2,431 Points

yes, u're right. I have also forgotten the bracket at the end. Thx!