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 trialSathya Musanipalli
2,265 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
Treehouse Project ReviewerYou'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
231,275 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.