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!

When Warrior is converted to a string, the string should be like "Warrior, <weapon>, <attack_limi

i can't pass this task , i don't know why.

warrior.py
from character import Character

class Warrior(Character):
  weapon = 'sword'

  def rage(self):
    self.attack_limit = 20

    def __str__(self): 

    return string

2 Answers

Brian Jones
Brian Jones
5,803 Points

You need to return a formatted string from your __str__() method.

All Warriors will have a string starting with "Warrior" and then you’ll have two variable values based on properties of the class. So, one way to write the formatted string would be:

"Warrior, {}, {}".format(self.weapon, self.attack_limit)

Now when you call print(), for example, on an instance of Warrior() that has had your rage() method called on it, it should print out:

Warrior, sword, 20

(You also have some indentation problems as the code appears above, but those are presumably beside the point of your main question.)

Matthew Rigdon
Matthew Rigdon
8,223 Points

You coded this well, the only issue is your last step (step 4). The str is meant to print out a specific string that describes, or summarizes the class that it is located in. Your class is Warrior, so the str should print out:

Warrior, sword, 20.

This is all the information that is written inside of your class. So we aren't just returning 'string,' we want to return a specific string that describes the warrior.

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

The code I wrote above will print out the requirements. Notice that I wrote self.weapon and self.attack_limit. Self tells the code to find the locations inside of the Warrior class where you referred to weapon and attack_limit.