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

How do I keep getting "eat is not defined" when it clearly is?

I keep going over this one and I can't seem to figure it out. Why do I get the NameError: name 'eat' is not defined?

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

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

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

panda_1 = Panda('Bao Bao', 12)

eat(panda_1)

1 Answer

Hi Devin Hight

"eat" isn't a global function call. It is a method on the panda class. In order for you to call eat(), you have to access it through the created panda object like so:

# other stuff from your class above

panda_1 = Panda('Bao Bao', 12)

panda_1.eat()

I hope that helps!