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

My brain has decided it isn't jumping anymore walls. What have I done wrong?? Thank you.

Create a method called eat. It should only take self as an argument. Inside of the method, set the is_hungry attribute to False, since the Panda will no longer be hungry when it eats. Also, return a string that says 'Bao Bao eats bamboo.' where 'Bao Bao' is the name attribute and 'bamboo' is the food attribute.

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

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

    def __eat__(self):
        self.is_hungry = False
        if self.is_hungry 
            return(f'{name} eats {bamboo}')

1 Answer

AJ Tran
STAFF
AJ Tran
Treehouse Teacher

Hi Aaron, there's just a few adjustments needed :)

First I will show you my results of running your code

>>> class Panda:
...     species = 'Ailuropoda melanoleuca'
...     food = 'bamboo'
...     name = 'Bao Bao'
...     def __init__(self, name, age):
...         self.is_hungry = True
...         self.name = name
...         self.age = age
...     def __eat__(self):
...         self.is_hungry = False
...         if self.is_hungry
  File "<stdin>", line 11
    if self.is_hungry
                    ^
SyntaxError: invalid syntax

SyntaxError usually means there is some punctuation missing.

Remember that "if statements" need to end in a colon!

if self.is_hungry:

Let's try it again:

>>> class Panda:
...     species = 'Ailuropoda melanoleuca'
...     food = 'bamboo'
...     name = 'Bao Bao'
...     def __init__(self, name, age):
...         self.is_hungry = True
...         self.name = name
...         self.age = age
...     def __eat__(self):
...         self.is_hungry = False
...         if self.is_hungry
...             return(f'{name} eats {bamboo}')
... 
>>> p = Panda('Mimi', 2)
>>> p.is_hungry
True
>>> p.eat()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Panda' object has no attribute 'eat'

This time, the class definition is syntactically correct. We can instantiate a new Panda instance p, and check if p is hungry.... but p cannot eat!

Let's break down the Traceback and it's error.

>>> p.eat()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Panda' object has no attribute 'eat'

AttributeError says that eat does not exist! But we have a definition for it....

def __eat__(self):

So there must be something in this line that needs to be fixed.

I'll leave you here on a new starting point to investigate :) If you need more help, we will be around for you!