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

Mohamed Anser Ali Ameerali
Mohamed Anser Ali Ameerali
2,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!.

first_class.py
class Student:
    def praise(self):

        name = "Anser"
        return "I really like your hair today, "+name+"!"




student1 = Student()
print(student1.praise())

2 Answers

Hi Mohamed,

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
Mohamed Anser Ali Ameerali
Mohamed Anser Ali Ameerali
2,904 Points

I tried

class Student: def praise(self):

    name = "Anser"
    return "I really like your hair today, "+name+"!"

student1 = Student() print(student1.praise())

but my answer was wrong,when I replaced my student class with your's I got it right,how it is ? what is the issue with my code?

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'