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

Tucker Fischer
Tucker Fischer
10,517 Points

Create a method called eat. It should only take self as an argument.

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.

Kinda stumped here. I've tried print instead of return but both are wrong.

Here is my code: 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 bamboo')

bao_bao = Panda('Bao Bao', 18) print(bao_bao.eat())

3 Answers

boi
boi
14,242 Points

Your one step away. Read the instructions carefully.

"Also, return a string that says 'Bao Bao eats bamboo.' where 'Bao Bao' is the name attribute and 'bamboo' is the FOOD ATTRIBUTE."

It says bamboo should be a food attribute.

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 bamboo') 👈# Here.

Also, make sure you have a period (.) at the end of the string, and make sure you have a space after return. If you need help, let me know.

Ephrain Reliford
Ephrain Reliford
2,981 Points

change the very last line to:

return "{} eats {}.".format(self.name, self.food)

The extra parenthesis throws off the challenge checker

Stephen Cole
PLUS
Stephen Cole
Courses Plus Student 15,809 Points

The wording of this challenge makes it harder than it needs to be.

It should ask you to return a string that is created using the variables name and food.

Asking for a specific string implies that using that string will work. It doesn't.

The modern way (with F-strings):

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