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

What'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?

panda.py
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
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Hey 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!!!

I solve it

I put the code in the right position :-)

Thank you!

i'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
Chris Freeman
Treehouse Moderator 68,423 Points

Hey 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
Gunter Ostendorp
1,740 Points

For 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
Chris Freeman
Treehouse Moderator 68,423 Points

Hey 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!!!

ohhhh!!! well when you put it that way now I get it! Thanks!!