Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Basic Object-Oriented Python Welcome to OOP Adding to our Panda

gabrielalcaraz
gabrielalcaraz
7,570 Points

I dont know what I'm doing wrong

I try to add what I think I'm supposed to do and the error message isnt helping me, help?

panda.py
class Panda:
    species = 'Ailuropoda melanoleuca'
    food = 'bamboo'

    def __init__(self, name, age):
        self.is_hungry = True
        self.name = name
        self.age = age

    def eat(self):
        self.is_hungry = False
        return f'{self.name} eats {self.food}.'

    def check_if_hungry(self):
        if is_hungry == True:
            self.eat()


panda = Panda('Bao Bao', '5')

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,464 Points

Hey gabrielalcaraz, you are SO close!!

An instance attribute must be referred to using self., as in self.is_hungry. The variable is_hungry is not defined.

Post back if you need more help. Good luck!!

gabrielalcaraz
gabrielalcaraz
7,570 Points

coding feels like a mountain when your actually climbing a hill, it feels hard then when you see the answer you feel like an idiot

Chris Freeman
Chris Freeman
Treehouse Moderator 67,464 Points

It's a hard one to catch because is_hungry not strictly a undefined variable name until the code runs. If is_hungry was defined at the module level, the code would run. If not defined, it raises a NameError. With practice, you will naturally ask yourself where every variable is defined (local to the method, local to the class, local to the module, or globally).

Everyone, even Treehouse teachers, have made and caught this error in coding.