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!

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