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 trialMałgorzata Staniszewska
2,431 PointsWhy doesn't it work?
?
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
Treehouse TeacherHi 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!
Małgorzata Staniszewska
2,431 PointsI'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
Treehouse TeacherTake 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):
Małgorzata Staniszewska
2,431 Pointsyes, u're right. I have also forgotten the bracket at the end. Thx!