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 Method Interactivity

Ingrid Matthews
Ingrid Matthews
1,465 Points

attributes of class

With the attached code, I get an error message "couldn't find 'Student'". Previously I had the grade = self.grade line in my feedback method, which returned an error message "Student has no attribute 'grade"". I've been trying endless permutations of this and can't get it right.... grateful for any help.

first_class.py
class Student:
    name = "Your Name"
    grade = self.grade

    def praise(self):
        return "You inspire me, {}".format(self.name)

    def reassurance(self):
        return "Chin up, {}. You'll get it next time!".format(self.name)

    def feedback(self, grade):

        if int(grade) > 50:
                 praise(self)
        elif int(grade) <= 50:
                 reassurance(self)

Hi Ingrid,

Can you post the question?

2 Answers

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 38,004 Points

You were close when you had it in feedback but have it backwards. It needs to be self.grade = grade. grade is what is being passed in and you want to set it equal to self.grade.

Also you want to return the values from praise() and reassurance().

And will need self. when you call the praise() and reassurance() methods

Ingrid Matthews
Ingrid Matthews
1,465 Points

Thanks a lot. Does this mean there should be a line in the feedback function saying "self.grade = grade.grade"? Is there a video that explains this further?

Is it not already part of the praise and reassurance functions to return their values? Again, thanks..

Mark Sebeck
Mark Sebeck
Treehouse Moderator 38,004 Points

Yes praise() and reassurance() do return the value but you need to return the value they return from feedback(). Otherwise when you called feedback() you would not get any output.

I don't think you need self.grade = grade. You would only if you needed to set the class attribute to the grade passed in. But since you only use grade in the method feedback() you don't need to. I just noticed you had it backwards and didn't really look into if it was needed.

grade is what is passed into the method. self.grade is a class attribute that could be used as part of the class. Hope this helps.

unknown member
PLUS
unknown member
Courses Plus Student 18,949 Points
class Student:
    name = "Your Name"

    def praise(self):
        return "You inspire me, {}".format(self.name)

    def reassurance(self):
        return "Chin up, {}. You'll get it next time!".format(self.name)

    def feedback(self, grade):
        if grade > 50:
            return self.praise()
        else: 
            return self.reassurance()
unknown member
unknown member
Courses Plus Student 18,949 Points

Code snipet above is a little too short to explain the whole thing. You should create an object using student class (it is a blueprint for an actual object that you'll create) and try to play a little with the code, create more methods in the class which can do just anything, it doesn't matter. Afterwards create and object like:

     Ingrid = Student()
     Ingrid.praise()
     Ingrid.reassurance()
     Ingrid.feedback()
     Ingrid.any_method()

That "self" thing in your methods represents your object (in your case Ingrid). Your objects can have properties (in the challenge it was ' name = "Your Name" ') and methods, you access them by dot (.) notation, you make your objects do things or accommodate values about itselves. Later in the course you will encounter init method and maybe after that it will make a little more sense.