This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by mateon1
about 11 years

2.1: "NameError: name 'is_hungry' is not defined" yet I pass the test

My code:

# Class definition
class Animal(object):
    """Makes cute animals."""
    # For initializing our instance objects
    def __init__(self, name, age, is_hungry):
        self.name = name
        self.age = age
        self.is_hungry = is_hungry

# Note that self is only used in the __init__()
# function definition; we don't need to pass it
# to our instance objects.

zebra = Animal("Jeffrey", 2, True)
giraffe = Animal("Bruce", 1, False)
panda = Animal("Chad", 7, True)

print zebra.name, zebra.age, zebra.is_hungry
print giraffe.name, giraffe.age, giraffe.is_hungry
print panda.name, panda.age, panda.is_hungry

Returns:

Traceback (most recent call last):
  File "python", line 2, in <module>
  File "python", line 8, in Animal
NameError: name 'is_hungry' is not defined

I still pass the excercise but that’s annoying cause I can’t see the code in action.

Answer 51596ba41431743782001354

9 votes

Permalink

Same here: it must be some trouble with the validator that I’m going to report now.

You seem to have the correct code, so rest assured and go on :)

Oh, on a side note it is just a matter of indentantion: cancelling the indentation at line 8, then riputting the tabs just works fine.

points
Submitted by Giacomo Sorbi
about 11 years

9 comments

Alex J about 11 years

When I copy&paste the code into the exercise (on the old interface), it validates as correct.

Nick Reilingh about 11 years

The only way I could get this to validate without error was to convert all the indents to spaces in an external editor and then paste back in. Goddamn editor and style inconsistencies! XO

I had to cancel all the indentation and input it again, but that fixed it :)

Strange, as navigating it, it looked like all tabs…

Giacomo Sorbi almost 11 years

I think it had some space hidden among tabs. Just guessing, though.

Daniel Seymour almost 11 years

Thanks for letting me know about the indentation. Did you report this to the buggy exercises group?

Daniel Seymour almost 11 years

PS All that was for Giacomo.

Giacomo Sorbi almost 11 years

Yeah, we already discussed it in the buggy exercise group: it appears it happens with some old exercise when switching to the new UI.

Li Sophia almost 11 years

It is the problem of the indentation from line 2 to 8.

Giacomo Sorbi almost 11 years

Well, try doing tabula rasa: completely cancel every indentation and put it anew, so to fix it.

Answer 516d6fd188e7d398a2000346

9 votes

Permalink

I find this problem is about the space area at the line 2,3,4,5, I delete the space,and use Enter at the line 2,3,4,5 ,this problem is solved.

points
Submitted by jaychang
about 11 years

2 comments

lxt almost 11 years

Thanks, it helps for me .

Mo Alawi almost 11 years

Thanks a lot, a bit strange when you try to fix a fault you cant see !

Answer 515dc1bd09019cf09f000024

1 vote

Permalink

Wht you suggested worked re the indentation, thanks

points
Submitted by Spwnies
about 11 years

2 comments

Martin Demling about 11 years

Same for me.

Kevin James Oli almost 11 years

Same here too!

Answer 5160d6104919a138fc002747

0 votes

Permalink

Giacomo: I’m having the same problem, and I wasn’t able to fix it by deleting the existing indentation at line 8 and then inserting the tabs. It isn’t critical, but it is irritating… half the fun is solving the problem, and the other half is seeing the code work ;) Ciao! Daniel

points
about 11 years

1 comments

Giacomo Sorbi about 11 years

I tried just now and it works fine to me: try checking also other indentations, particularly on line 7.

Answer 516662eaa876694ac200241b

0 votes

Permalink

Yes the problem was with the indentation. I had to fix everything that is indented. I see this problem often when I copy some code from the classes course and paste it in Codecademy Labs. Codecademy Labs always has problem with the indentation of code in this course it seems.

points
Submitted by Bogdan Stoichkov
about 11 years

Answer 5186742e93b555ef89004344

0 votes

Permalink

I have the same problem but i dont pass. i dont know why though.

points
Submitted by James Shakow
almost 11 years

1 comments

Giacomo Sorbi almost 11 years

Remove any indentation up to line 8 and then put it again, it should fix it.

Answer 5193f667692e241f2b000cce

0 votes

Permalink

Still having this problem :(

points
Submitted by Sara
almost 11 years

1 comments

Giacomo Sorbi almost 11 years

Oh, Euridice - great nick. Tell me what error the system gives you and we’ll see what we can do.

Remember that also resetting the indentation on the line before the one in the error helps a lot.

Answer 536235788c1ccc5fbd001892

0 votes

Permalink

I don’t see any problem with your code. It’s only because I’m using Chrome. If you’re not using Chrome, then I don’t know what the problem is.

points
Submitted by William Sanborn
almost 10 years