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 trialMichael Cook
5,864 PointsStuck on a Class (self) related code challenge. I think I have the correct code?
Hi, so right now I'm learning about Python classes, which is quite cool actually. I've been enjoying it a lot! I just recently got to this coding challenge that wants me to take the 'class Student' (which has a 'name' attribute) and define a praise method, which all it tells me to do is return a nice positive message using the 'name' attribute. It also wants me to use an instance of the class.
So down below is my code, in my 'praise' method I used the 'self' argument (which it also wants me to do), and I have it return a print message, which then calls 'Student.name'.
Whenever I run this code through the code challenge, it just gives me a "Oh no! You forgot the self
argument in your praise
method". However I'm not sure where I'm forgetting one, I have it in my arguments and it seems to work fine. Do I need to add another one?
When I run the code in my workspace, it seems to print out the desired result. The short praise message with the name included. So I'm a little confused on what's going on, sorry if I'm missing something obvious hehe. Thank you!
class Student:
name = "TestName"
def praise(self):
return print("You're doing great {}!".format(Student.name))
#end
me = Student()
me.praise()
2 Answers
Pedro Cabral
33,586 PointsYou have two mistakes. The first one is that your praise should return a string, and not the return value of the print method. In other words, you don't need the print method; The second thing is that to access an instance's attribute you should call it using self with self.attribute
Michael Cook
5,864 PointsOkay, it worked. Thanks a lot!!