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!

Warrior 4 of 4

from character import Character

class Warrior(Character): weapon = "sword" pass def rage(self): self.attack_limit = 20

def str(self):
string = "{}, <{}>, <{}>".format(string, weapon, rage()) return string

warrior.py
from character import Character

class Warrior(Character):
  weapon = "sword"
  pass
  def rage(self):
    self.attack_limit = 20

  def __str__(self):  
    string =  "{}, <{}>, <{}>".format(string, weapon, rage())
    return string

MY error message is "try again"

7 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Oh, hey, I couldn't see this on my phone well enough to spot the error. So, few things:

  1. Indentation, like I said on Twitter. But you said you got that so, on to number 2.
  2. You're calling rage with rage(). That would be fine if rage was a function and not a method. You need to call self.rage(). You also want self.weapon instead of just weapon, since these things belong to the instance.
  3. string doesn't exist when you do .format(). Not sure what you're expecting to come in there. Maybe self.__class__.__str__()? It's probably easier and safer to just do "Warrior".

I tried this code and got same error

def __str__(self):  
    string =  "{}, <{}>, <{}>".format(string, self.weapon.__str__, self.rage.__str__())
    return string
Kenneth Love
Kenneth Love
Treehouse Guest Teacher
def __str__(self):
    string = "{}, <{}>, <{}>".format(
        string,
        self.weapon.__str__,
        self.rage.__str__()
    )
    return string

self.weapon is already a string, so you don't need to use .__str__ on it. That's not going to get you a string anyway, it's going to get a reference to a bound function (aka method). Similar error for the self.rage.__str_().

And what's your thinking behind the string that is your first argument to format?

Your answer looks perfect but I get same message even though I copy paste it.

from character import Character

class Warrior(Character):
  weapon = "sword"
  pass
  def rage(self):
    self.attack_limit = 20

  def __str__(self):
    string = "{}, <{}>, <{}>".format(
        string,
        self.weapon.__str__,
        self.rage.__str__()
    )
    return string

It is keep saying try again.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Well, mine is exactly the same as yours, just syntax highlighted and slight format changes.

You haven't answered my question, though. Can you walk me through each line? What are you expecting to happen at each step of the __str__ method?


I could just give you the answer, of course, but I want to know what I missed that get you confused so I can fix it for you and other students. You're really really close.

@Kenneth Love

The question is asking me in this part of challenge level 4 of 4.

  1. warrior is converted to string. " "

  2. string is = to "warrior, weapon, rage"

  3. update your class to produce this string ("") using values from instance

  4. those will be string, self.weapon.str, self.rage.str()

  5. when string return, it will return the values from instance

    still can't pass.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

There's nothing in the challenge about a variable named string or about using the __str__ method for either weapon or rage.

Let's pretend I have a Warrior instance named Furiosa.

furiosa = Warrior()

How would I get to Furiosa's attack_limit?

How would I get to her weapon?

Now, can you use those answers to fix the __str__ method in your class?

(And you don't need the string variable. Just put the word Warrior in your string before the two {}s for weapon and attack_limit)

from character import Character

class Warrior(Character):
  weapon = "sword"
  pass
  def rage(self):
    self.attack_limit = 20

  def __str__(self):  
    string =  "Warrior, {}, {}".format(weapon, rage())
    return string

error: try again

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

Finally this code passed.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

That's it! Congratulations! Good job pushing through.

Thanks Kenneth!

got it thanks

def str(self): string = "Warrior, {}, {}".format( self.weapon, self.attack_limit ) return string

THIS ONE WORKS FOR ME

this work for me.

def str(self): string = "Warrior, {}, {}".format(self.weapon, self.rage()) return string