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 trialKevin Lozandier
Courses Plus Student 53,747 PointsCan'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
Jason Anello
Courses Plus Student 94,610 PointsHi 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
Kenneth Love
Treehouse Guest Teacherself
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.
Kevin Lozandier
Courses Plus Student 53,747 PointsThanks for clarifying that, Kenneth!
Kevin Lozandier
Courses Plus Student 53,747 PointsKevin Lozandier
Courses Plus Student 53,747 PointsThanks for realizing I forgot to use
return
; I am still a bit confused when doesself.weapon
get set, though I'm sure that's a detailCharacter
took care of but it wasn't shown for some reason as another tab of code to view.Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsYou'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 accessattack_limit
usingself
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.