Welcome to the Treehouse Community
Looking to learn something new?
Mohamed Anser Ali Ameerali2,904 Points
Problem with object
I tried this program in my pc it is working,but i'm unable to get it right in Team house ide,the error show me is didn't find name in praise,please use instance attribute!.
class Student: def praise(self): name = "Anser" return "I really like your hair today, "+name+"!" student1 = Student() print(student1.praise())
Yeah, your code does work! But you've put the name attribute inside of the praise method. And they want that attribute to be part of the class, not part of the method.
So you can fix that by writing your method below it, and use self.name inside to point it to the right attribute of the class.
Hopefully this makes sense!
class Student: name = "Your Name" def praise(self): return "Great job, " + self.name
There is not really an issue with your code, the method does work!
But since this is a Student class you probably want all your attributes (in this case name) to be part of the class itself. This way you can use your attributes in all the methods you create for this class.
See, I can still access the name attribute in my code:
class Student: name = "Harry" def praise(self): return "Great job, " + self.name student1 = Student() print(student1.name) # gives me back 'Harry'
But in your code, you can't access or reuse it because you made it part of your method.
class Student: def praise(self): name = "Anser" return "I really like your hair today, "+ name +"!" student2 = Student() print(student2.name) # This gives: AttributeError: 'Student' object has no attribute 'name'