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

Andrew Lynford
Andrew Lynford
1,906 Points

Creating methods in classes.

Hi, I am having trouble with this quiz question. Why does a syntax error occur in def eat method I am trying to create? Also, I am not sure where I should define the panda name attribute, so I put it inside class attribute body which likely makes no sense. Any advice?

Thanks. :)

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


    def __init__(self):
        self.is_hungry = True

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

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

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,464 Points

Hey Andrew Lynford, the error is in your attribute references.

Attribute within a class instance need the self. prefix. This is the purpose of the first parameter self. It makes the connection from the current executing code and the instances it belongs to.

  • change name to self.name1 and food to self.food

Also, the checker is persnickety about spacing. There needs to be a space after return. It is not a function so the parens are not needed. Parens are allowed if grouping a complex statement.

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