What does NameError mean?


#1



https://www.codecademy.com/en/courses/python-beginner-2W5v7/1/5?curriculum_id=4f89dab3d788890003000096

Hello. I was playing around with the PygLatin translator when I got stuck. I can't see what is wrong with my code,and I hope someone else can help me to debug this.

For example, when I input JimM9, I got the error messsage:

Traceback (most recent call last):
File "python", line 14, in
NameError: name 'new_word2' is not defined

This is my code:


yg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    word = original.lower()
    first = word[0]
    new_word = word[1:len(new_word)] + first + pyg
    print new_word
    
else:
    word2 = original.upper()
    second = word2[1]
    new_word2 = word2[1:len(new_word2)] + second + pyg
    print new_word2


Thanks in advance! :slight_smile:


#2

Hi the erro come from here

word[1:len(new_word)]

you didn't declare new_word before so instead of it you should put original the same for the one inside the else statement then

yg = 'ay'

it shoudl be pyg instead of yg


#4

Wait I'm so sorry, but I went to try again. Why does this code work then, since new_word was also not defined before doing the len thing? Thanks!

pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
new_word = word[1:len(new_word)] + first + pyg
print new_word

else:
print 'empty'


#5

its doesn't work if you out a nam that doesn't have a number

but if you put a name with a number inside it will print "empty"


#6

But strangely I can get this, which confuses me tremendously:

:frowning:


#7

@stetim94 hiya do you mind helping out, please? Merci beaucoup! :slight_smile:


#8

what exactly is the question?


#9

The question is:

Why doesn't this code work?

pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    word = original.lower()
    first = word[0]
    new_word = word[1:len(new_word)] + first + pyg
    print new_word
    
else:
    word2 = original.upper()
    second = word2[1]
    new_word2 = word2[1:len(new_word2)] + second + pyg
    print new_word2

For example, when I input JimM9, I got the error messsage:

Traceback (most recent call last):
File "python", line 14, in
NameError: name 'new_word2' is not defined

wizmarco (thanks!) suggested that it is because I have not declared new_word before I used new_word = word[1:len(new_word)] + first + pyg, but I went back to an earlier excercise and it worked fine even without having to declare it first.

Thanks!


#10

both in this line:

new_word = word[1:len(new_word)] + first + pyg

and ths line:

new_word2 = word2[1:len(new_word2)] + second + pyg

you are using the variable (new_word and new_word2) while you are defining these variable, you can't do this.


#11

okay, I tried again today, and the same code refuses to work any longer. So I suppose both you and @wizmarco are right. Thanks a lot for taking the time to explain this!