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 trialJorgen Rasmussen
3,367 PointsCode challenge previous to this lesson:
This class should look familiar!
First, I need you to add a method named praise to the Student class. It should take the self argument. Then, inside the praise method, return a positive message about the student using 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!
I write the following: class Student: name = "Your Name"
def praise(self):
return "You rock,{}!".format(name)
And I get an error message saying: Bummer: NameError: name 'name' is not defined
but I defined name = Your Name
What am I missing?
2 Answers
Jorgen Rasmussen
3,367 PointsHello Jesus, That did it, thanks! So I had to put self because that is the name of the instance that will be created which the attribute name belongs to? Thanks again
Jesus Reyes
1,946 Pointsyep, every time you create a class, all "variables" created directly in the class will be attributes, while you are declaring in the root of the class, in this case name
, you write it as a variable, and in every method you call it or reference it as self.<attribute name>. Is not explicitly the "name" of the instance on the class created, that will be the variable were you assign the instance to, is more the reference of that instance to access the attributes and methods inside.
Jorgen Rasmussen
3,367 PointsThanks again Jesus, your description helps me understand what is going on here Take Care :)
Jesus Reyes
1,946 PointsJesus Reyes
1,946 Pointsim assuming that
name
is an attribute in your class, if so, you need to call it asself.name
instead ofname
so your code should look something like: