Please tell me what is wrong with this code


#1

Hi,

I am not sure if this piece of code is right:

pyg = "ay"
original = raw_input('SABYASACHI')
new_word = word[1:10] + fifth + pyg
if len(original) > 0 and original.isalpha():
word = original.lower()
fifth = word [4]
print "first"

else:
print 'empty'

I'm getting this response:
Traceback (most recent call last):
File "python", line 3, in
NameError: name 'fifth' is not defined

But I have defined fifth as word [4]. Can't understand this. Somebody please help me with this.


#2

I'm very new here but here's what i think,

You need a : for
original = raw_input('SABYSACHI')
should be
original = raw_input('SABYSACHI: ')


#3

You are trying to use the variable fifth before you have declared it. You declare it after trying to use it.


#5

I think you should declare your variable fifth before your new_word variable. Because Python executes your statements from top-to-bottom, Python doesn't know what fifth is in new_word. Therefore, you encountered the NameError! For possible future reference, you shouldn't define a variable after you've used it in previous statements.


#6

Thanks so much for your help. I figured out the correct code after first declaring the variable. It is now working. :slight_smile:


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.