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!

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

McKenzie Klasing
McKenzie Klasing
1,720 Points

Using Dot Notation to access an attribute in class Panda - receiving: NameError: name 'Panda' is not defined

This is my first time asking a question so I apologize if it isn't formatted correctly.

This Challenge task reads as follows: 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.

I receive this error: NameError: name 'Panda' is not defined regarding line 12, return str(f'{self.name} eats {Panda.food}')

I am able to get this code to run in my own IDE (mu) and it prints "Bao Bao eats bamboo" which tells me Panda.food is working properly. I've tried numerous things to fix this like using self.food but of course that doesn't work because this is a class attribute, not an instance attribute... Am I getting this wrong or is the task grader/checker looking for it to be written a different way?

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 str(f'{self.name} eats {Panda.food}')

panda_one = Panda('Bao Bao', 4)


1 Answer

Travis Alstrand
.a{fill-rule:evenodd;}techdegree seal-36
Travis Alstrand
Data Analysis Techdegree Graduate 45,427 Points

Hey there McKenzie Klasing ! :wave:

So one very sneaky thing I just noticed was at one point I had to look in the Test Results at the error and it mentions to "make sure there is a period".

We also don't need to use the str() method, we can just straight up return the f-string and use self.food

return f'{self.name} eats {self.food}.'
McKenzie Klasing
McKenzie Klasing
1,720 Points

Awesome thanks Travis!

That did work! I also had to remove the print statement at the end. I appreciate you taking the time to assist me with this-