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

NameError issue:

I have tried rearranging my code and nothing seems to work.

panda.py
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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Megan Amendola
Treehouse Teacher

The 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.

Megan 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.

This 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

Got 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
Megan L
2,905 Points
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
        return f'{self.name} eats {self.food}.'

    def check_if_hungry(self):
        if self.is_hungry:
            return self.eat()