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!

Can't seem to sole last task of last challenge of the OOP Python course, desired output string is seemingly too vague.

Based on the __str__ output desired, it's seemingly too vague as "Weapon, <weapon>, <attack_limit>" for me so thus far.

It's unclear whether weapon and attack_limit are to be enclosed by the square brackets or not. Perhaps Kenneth Love or other challenge implementers clear up what is the desired output of __str__?

Nonetheless, all attempts of me solving the challenge with the following code snippet that was able to allow me to easily solve the other tasks have failed.

from character import Character
class Warrior(Character):
    weapon = 'sword'

    def rage(self):
      self.attack_limit = 20

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

2 Answers

Hi Kevin,

You have the right string. Your only issues are that you're not returning the string from the method and not using self.weapon

Thanks for realizing I forgot to use return; I am still a bit confused when does self.weapon get set, though I'm sure that's a detail Character took care of but it wasn't shown for some reason as another tab of code to view.

You're welcome.

In task 2 is when you set the class variable weapon equal to 'sword'. So when you go to pass it into the format method you have to access it with self in the same way that you have to access attack_limit using self

I think that when you have weapon by itself inside the __str__ method it's treated as a local variable which doesn't have a value.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

self just means it applies to the current instance. All attributes defined on a class belong to their instances, so setting weapon = 'sword' in step 2 creates self.weapon on every instance.

Thanks for clarifying that, Kenneth!