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 trialsophiawong
9,104 PointsWhat's wrong in my code, please?
I have set "Bao Bao" into the self.name. But, why is the system tell me it doesn't work?
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.name = 'Bao Bao '
self.is_hungry = False
return f'{self.name} eats {self.food}'
4 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsHey diginoma, the eat()
method is overriding the self.name
attribute. Delete the assignment to self.name
inside eat()
As it is now, it’s “Boa Boa eats”... all the time
Post back if you need more help. Good luck!!!
sophiawong
9,104 PointsI solve it
I put the code in the right position :-)
Thank you!
diginoma
Courses Plus Student 8,009 Pointsi'm having a similar problem... I get....
Traceback (most recent call last): File "", line 67, in test_results AssertionError: 'Bao Bao eats berries.' != 'Charles eats berries.'
- Bao Bao eats berries.
- Charles eats berries. : Make sure to use the attributes already in the class. When I create an instance with Charles as the panda's name and change the food attribute to berries, I don't get the right message.
whats the real problem here?
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.name = 'Bao Bao'
self.is_hungry = False
return f'{self.name} eats {self.food}.'
Chris Freeman
Treehouse Moderator 68,457 PointsHey diginoma, the eat()
method is overriding the self.name
attribute. Delete the assignment to self.name
inside eat()
As it is now, it’s “Boa Boa eats”... all the time
Post back if you need more help. Good luck!!!
Gunter Ostendorp
1,740 PointsFor this what I wound up doing is calling the class under the eat method and passing in the name argument instead of defining it in the method. That way you could change it in each separate instance instead of the name always being Bao Bao every time you call the eat method.
Chris Freeman
Treehouse Moderator 68,457 PointsHey Gunter Ostendorp, it’s the __init__
method that sets the instances self.name
attribute so the name will be available and unique to that instance. Passing in a name would simply override the instance attribute and require the calling code to keep track of the name outside of the class. This defeats the purpose of having the name attribute.
Post back if you there’s more that I’m not seeing. Good luck!!!
diginoma
Courses Plus Student 8,009 Pointsohhhh!!! well when you put it that way now I get it! Thanks!!