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 trialManuja Jayawardana
1,450 PointsHow can i get past this ?
Ive been stuck here it seems so simple i dont see whats wrong
class Student:
name = "Manuja"
def praise(self):
return "Well Done %s"%self
name = Student()
8 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsYou are very close! Format the praise string with self.name
Manuja Jayawardana
1,450 PointsThank you! Could I ask why that's necessary when my variable is in fact "name"? Since I'm just replacing a placeholder, why is the "self" part necessary? Thank you so much!
Chris Freeman
Treehouse Moderator 68,457 PointsThis is a more advanced question. If you donβt understand the following, come back after classes have been covered in more detail.
When working with an instance of a class, the self
points to the instance under consideration. To access the name
attribute of this instance, the "dot" notation is used: self.name
.
By using just "self" the format would try use the instance of Student. This fails because Student
doesn't have a __str__
method to return a value when an instance of Student
is used in a string context such as this formatting case.
There is a way just "self" would work, but you might not understand it until you cover magic methods. The following code would work if a __str__
method is added:
class Student:
name = "Manuja"
def praise(self):
return "Well Done %s"%self
def __str__(self):
return self.name
In the above code, self
is used in a text context, which cause a call to the instance __str__
method, which returns self.name
. This value can successfully be used in the string format!
Gerard Nwazk
Courses Plus Student 2,833 Pointsclass Student:
name = "Gerard"
def praise(self):
return("You're doing a great job {}".format(self.name))
Michael Moore
7,121 PointsFor anyone following the Python track, your answer is the best. We haven't covered the init or str stuff yet. +1, thanks. I was printing instead of returning, d'oh!
Kevin Koch
Full Stack JavaScript Techdegree Student 3,767 PointsYour solution is more helpful than.... Chris almost got my brain f up
Chris Freeman
Treehouse Moderator 68,457 PointsIf one just wants the answer without an understanding of why, a cut-and-paste solution will always seem better. π
Michael Roberts
2,091 PointsNone of the above solutions passed for me. This was the one which passed and didn't use any functions/features we haven't learned yet:
class Student:
/n
name = "Your Name"
/n
def praise(self):
/n
return "Well done {}!".format(self.name)
Dinu Sirbu
2,063 PointsThanks for sharing your solution
pierreilyamukuru
9,831 Pointsclass Student: name = "Pierre ilyamukuru" def praise(self): return "You're doing a great job, {}!".format(self.name)
Raymond Tapiwa Shoniwa
3,931 Pointsthis is how i got it, def praises(self): name = "Tipsy" return "You're doing great{}!".format(self.name)
Logan Eppley
Courses Plus Student 26,697 Pointsclass Student:
name = "Your Name"
def __init__(self, name):
self.name = name
return("You are doing great, {}".format(name))
Would this not accomplish the same thing?
Chris Freeman
Treehouse Moderator 68,457 PointsThe __init__
should not return a value. Running your code produces:
>>> s = Student("bob")
Traceback (most recent call last):
File "<string>", line 7, in <module>
TypeError: __init__() should return None, not 'str'
And the praise
method would still need to reference self.name
ARNOLD TINASHE NZIRA
12,269 Pointsclass Student: name = "Your Name" def init(self, name): self.name = name return("You are doing great, {}".format(name))
me = Student()
Mercedes Aker
6,544 Pointsclass Student: name = "Sadie"
def praise(self): return "Well Done!, {}!".format(self.name) def str(self): self.name
Anyone see the problem here?
Manuja Jayawardana
1,450 PointsManuja Jayawardana
1,450 PointsWhoops sorry, the question is - """ This class should look familiar! I need you to add a method name praise. The method should return a positive message about the student which includes the name attribute. As an example, it could say "You're doing a great job, Jacinta!" or "I really like your hair today, Michael!". Feel free to change the name attribute to your own name, too! """