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.

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

why doesn't this str method work?

Why doesn't the str method work in this code?

warrior.py
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))

I 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.

Yeah, I tried that. No dice.

def str(self): print('Warrior, {}, {}' .format(self.weapon, self.attack_limit))

Too bad. I would personally always use %s so I don't know {} formatting well.

1 Answer

from 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."