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 Basic Object-Oriented Python Welcome to OOP Adding to our Panda

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 68,441 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!!

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 68,441 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.