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 trialAdrian Brown
Python Development Techdegree Graduate 14,496 PointsNameError issue:
I have tried rearranging my code and nothing seems to work.
class Panda:
species = 'Ailuropoda melanoleuca'
food = 'bamboo'
name = 'Bao Bao'
def __init__(self, name, age):
self.is_hungry = True
self.name = name
self.age = age
def eat(self):
if self.is_hungry:
self.is_hungry = False
return f'{name} eats {food}.'
5 Answers
Megan Amendola
Treehouse TeacherThe error you are receiving is: NameError: name 'name' is not defined. You don't have a name variable in your class or inside of the eat method, you do have self.name
and self.food
.
Nischal Shrestha
2,027 PointsMegan Amendola . Hey, but we cannot define self.food, as the question asked us to let init to take only 3 arguments and eat only self.
Megan Amendola
Treehouse TeacherHi Nischal Shrestha ! food
is a class attribute, but you access it inside of the eat method with self.food
.
Kat Alicea de Leon
1,787 PointsThis is not working for me either:
class Panda: species = 'Ailuropoda melanoleuca' food = 'bamboo' name = "Bao Bao" age = 1
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}'
This is the error message I get:
Task #1
.
Ran 1 test in 0.002s
OK
.
Ran 1 test in 0.000s
OK Task #2
.
Ran 1 test in 0.001s
OK
F
FAIL: test_results (main.TestFunctionDefinitionExecution)
Traceback (most recent call last): File "", line 64, in test_results AssertionError: 'Bao Bao eats bamboo' != 'Bao Bao eats bamboo.'
- Bao Bao eats bamboo
- Bao Bao eats bamboo. ? + : When I call the eat method, I don't get the correct message. Make sure to use the name attribute that is passed in to the instance.
Ran 1 test in 0.001s
FAILED (failures=1)
.
Ran 1 test in 0.001s
OK
.
Ran 1 test in 0.000s
OK
Dennis Amiel Domingo
17,813 PointsGot it!
class Panda:
species = 'Ailuropoda melanoleuca'
food = 'bamboo'
name = 'Bao Bao'
def __init__(self, name, age):
self.is_hungry = True
self.name = name
self.age = age
def eat(self):
self.is_hungry = False
name = self.name
food = self.food
return f'{name} eats {food}.'
panda_one = Panda('Bao Bao', 10)
panda_one.eat()
Megan L
2,905 Pointsclass Panda:
species = 'Ailuropoda melanoleuca'
food = 'bamboo'
name = 'Bao Bao'
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}.'
def check_if_hungry(self):
if self.is_hungry:
return self.eat()