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 trialAndrew Lynford
1,909 PointsCreating 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. :)
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
Treehouse Moderator 68,454 PointsHey 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
toself.name1
andfood
toself.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!!