Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Beth Singley
3,491 Pointswhy doesn't this str method work?
Why doesn't the str method work in this code?
from character import Character
class Warrior (Character):
weapon = 'sword'
def rage(self):
self.attack_limit = 20
return self.attack_limit
def __str__():
print ("Warrior, {}, {}" .format(weapon, self.attack_limit))

Beth Singley
3,491 PointsYeah, I tried that. No dice.
def str(self): print('Warrior, {}, {}' .format(self.weapon, self.attack_limit))

jacinator
11,924 PointsToo bad. I would personally always use %s so I don't know {} formatting well.
1 Answer

lambda
12,556 Pointsfrom character import Character
class Warrior (Character):
weapon = 'sword'
def rage(self):
self.attack_limit = 20
return self.attack_limit # This line is not necessary, it says to "set attack_limit to 20" which you did on the line above
def __str__():
print ("Warrior, {}, {}" .format(weapon, self.attack_limit))
# 1) ^ there is a space between the end of the string and the "."
# 2) but the main issue is that "print" doesn't return anything.
# you want a __str__ to return the string representation of your Warrior object.
# 3) the first argument to all class method's should be "self"
# 4) you need to reference fields/properties of objects using "self."
jacinator
11,924 Pointsjacinator
11,924 PointsI think that you'll have to add the argument 'self' to str and assign weapon as self.weapon. I could be wrong, but that might fix it.