10.Ending up not sure why this is not working


#1



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

Enter a word: larry
Traceback (most recent call last):
File "python", line 8, in
NameError: name 'new_word' is not defined

I do not know why this code is giving me an error. what am i doing wrong here?


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'


#2

this line:

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

you want to set new_word, but you also want the length of new_word, which you haven't set yet. This is a bit of problem. You can't use variable before you created/set them.


#3

so i need to add the line
new_word = word + first + pyg
before the new_word = word[1:len(new_word)] + first + pyg.
I see. I altered the variable before i even created it. thank you so much


#4

if you do this:

new_word = word + first + pyg

which is good, you could upgrade this line:

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

you could just take a slice of new_word:

new_word = new_word[1:len(new_word)]

#5

thanks, i had the same problem


#6