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!

Dustin Joseph
Dustin Joseph
2,898 Points

When Warrior is converted to a string, the string should be like "Warrior, <weapon>, <attack_limit>". Update your class

When Warrior is converted to a string, the string should be like "Warrior, <weapon>, <attack_limit>". Update your class to produce this string using values from the instance.

I am not sure what i am doing wrong here. I have looked all over the forums about this question but cant find anything. Any help is appreciated.

warrior.py
from character import Character

class Warrior(Character):
  weapon = 'sword'
  string = 'Warrior'

  def rage(self):
    self.attack_limit = 20

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

My guess is that you're supposed to override the __str__ method that returns a string.

Basically, do what you're doing just make the method __str__ and return the string instead of printing something.

__str__ is what gets called whenever you try and print out an object. If there's nothing there, it basically just prints out the object name and the address in memory. What you're doing when you override the method is making the program call your special __str__ method whenever someone tries to print the object.

Dustin Joseph
Dustin Joseph
2,898 Points

David McCarty Thanks for the response! So i replace the print_warrior with str, is that what you meant? It didn't seem to work for me. (I apoligize if i miss something, its late at night haha)

Yes, what you need to do is replace print_warrior with __str__ and have it just return the string that you have setup in your original code.

Just return the string like this

def __str__(self):
  return "string {}".format(blah)
Dustin Joseph
Dustin Joseph
2,898 Points

Still not working. This is my code: def str(self): return "string {}".format(Warrior, self.weapon, self.attack_limit)

Im thinking it is something with how i am ending the code ^^

Dustin Joseph
Dustin Joseph
2,898 Points

I got it now. Thank you so much! :)

Yeah man, happy to help! Glad you got it working.

If you want, for others in the future, you can select my answer as the best answer so it's easier for others to see how you solves the problem :)

Dustin Joseph
Dustin Joseph
2,898 Points

I would choose you as best answer if i actually knew how haha

I believe there should be a button on the bottom of my first comment that says "best answer" with a little check mark next to it. :)

Dustin Joseph
Dustin Joseph
2,898 Points

Its not there for me. I read up on it and it says that if you reply as a comment and not an answer i am not allowed to select you as best answer? maybe that's it?

Huh, that's odd.

Well, no worries! :)

Just glad I was able to help you figure out the problem!

Take it easy

Dustin Joseph
Dustin Joseph
2,898 Points

Yeah thanks man. Have a good rest of your day :)

Ken Alger
Ken Alger
Treehouse Teacher

That's why the moderators are here.
:smile:

2 Answers

The problem is, you're not sticking with what they wanted you to have outputted.

You're doing:

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

And the problem is, you need it to come out as "Warrior, weapon, attack_limit"

All you really have to do at this point is change the string. Also, you don't need to put Warrior in the .format arguments, you just need the weapon and the attack_limit for two separate placeholders

Ken Alger
Ken Alger
Treehouse Teacher

Changed this comment to an answer

Oh cool! Thanks man!

Peter Rzepka
Peter Rzepka
4,118 Points

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.attack_limit)