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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Sathya Musanipalli](https://secure.gravatar.com/avatar/5c4754ab2a2f6d7bda6afbba82da6e8a?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Sathya Musanipalli
2,264 PointsI keep getting name error
It keeps saying that ‘name’ is not defined, but I have no idea how to define it as “Bao Bao”. It won’t work if I put the name “Bao Bao” in the string, so what am I doing wrong?
class Panda:
species = 'Ailuropoda melanoleuca'
food = 'bamboo'
def __init__(self, name, age):
self.name = name
self.age = age
self.is_hungry = True
def eat(self):
if self.is_hungry:
self.name = 'Bao Bao'
return f'{name} eats {food}.'
self.is_hungry = False
2 Answers
![Travis Alstrand](https://uploads.teamtreehouse.com/production/profile-photos/11658224/micro_travis.jpg)
Travis Alstrand
Data Analysis Techdegree Graduate 49,407 PointsYou're very close!
We'll need to move the is_hungry = False
line up and above the return
line though. As it will be unreachable code if it's below. Once something is returned, that function will stop running and code below it will not be ran.
You're already setting the name
property perfectly in your __init__
method, "Bao Bao" is just what the back end testing will send when it attempts to create a new Panda
. To access the name
and food
properties in that return line though, we'll need to use self.
👍
class Panda:
species = 'Ailuropoda melanoleuca'
food = 'bamboo'
def __init__(self, name, age):
self.name = name
self.age = age
self.is_hungry = True
def eat(self):
if self.is_hungry:
self.is_hungry = False
return f'{self.name} eats {self.food}.'
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsMost of Travis' suggestions are good, but the "eat" method doesn't need to test anything. It should always do the same thing when called.
In the next step, you will construct another method that will use a conditional.