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

taliarosen
taliarosen
2,492 Points

Assertion Error

I'm getting an error that what is returned is not correct for the objective but I don't see any issues.

The objective is: 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'

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

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

panda_one = Panda('Bao Bao', '12')
panda_one.eat()

2 Answers

Thomas Fildes
Thomas Fildes
22,687 Points

Hi,

This was puzzling me for a long time. I have looked at your code and can see that you have put parenthesis in your return function which is not required. The below code worked for me:

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.is_hungry = False
        return f'{self.name} eats {self.food}.'

panda2 = Panda('Bao Bao', 12)
panda2.eat()    

Hope this helps. Happy coding!

Tom

Steven Parker
Steven Parker
229,771 Points

Did you see my answer from yesterday?
Those 2 lines at the bottom might not break the challenge, but they still shouldn't be there!

I point this out because in some other challenges, doing "extra" stuff can indeed cause a failure.

Steven Parker
Steven Parker
229,771 Points

It's not necessary to put parentheses around the formatted string, and they don't cause a problem in themselves, but the challenge is expecting a space after the return keyword as you would need if you didn't have the parentheses.

Also, the challenge only asks you to add code to the class. You don't need to add any code to create an object instance or call any of the methods.

Thomas Fildes
Thomas Fildes
22,687 Points

Hi Steven,

Sorry for not getting back to you sooner. Yes you are correct the two bottom lines aren't required to pass the challenge. I just used them as part of my code to pass through it. At least a solution has been found to assist with this and thank you for your help and assistance with it.