This Code Seems Counter-Intuitive


#1

I’m confused that the code I pasted below actually worked. Specifically, in the second-to-last line of the code, I’m creating a variable called new_word, and simultaneously defining it by referencing itself. This seems counter-intuitive; wouldn’t I have to define the variable first, then later calculate its len()?

pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
  print original
else:
  print 'empty'

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

Where in my code do I make this new slice of new_word?
#2

running this code actually produces an error:

NameError: name ‘new_word’ is not defined

so it makes sense its counter intuitive, given it isn’t right.

besides, why is all this code:

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

not in the if clause? You only want to do this when user provides valid input