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 Instant Objects Your first method

Manuja Jayawardana
Manuja Jayawardana
1,450 Points

How can i get past this ?

Ive been stuck here it seems so simple i dont see whats wrong

first_class.py
class Student:
    name = "Manuja"
    def praise(self):
        return "Well Done %s"%self

name = Student()
Manuja Jayawardana
Manuja Jayawardana
1,450 Points

Whoops 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! """

8 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

You are very close! Format the praise string with self.name

Manuja Jayawardana
Manuja Jayawardana
1,450 Points

Thank 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
Chris Freeman
Treehouse Moderator 68,423 Points

This 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!

class Student:
    name = "Gerard"

    def praise(self):
        return("You're doing a great job {}".format(self.name))
Michael Moore
Michael Moore
7,121 Points

For 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!

Your solution is more helpful than.... Chris almost got my brain f up

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

If one just wants the answer without an understanding of why, a cut-and-paste solution will always seem better. πŸ‘

None 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
Dinu Sirbu
2,063 Points

Thanks for sharing your solution

class Student: name = "Pierre ilyamukuru" def praise(self): return "You're doing a great job, {}!".format(self.name)

this is how i got it, def praises(self): name = "Tipsy" return "You're doing great{}!".format(self.name)

class 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
Chris Freeman
Treehouse Moderator 68,423 Points

The __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

class Student: name = "Your Name" def init(self, name): self.name = name return("You are doing great, {}".format(name))

me = Student()

Mercedes Aker
Mercedes Aker
6,544 Points

class Student: name = "Sadie"

def praise(self): return "Well Done!, {}!".format(self.name) def str(self): self.name

Anyone see the problem here?