Pyglatin gives error message when empty


#1


Hi!

When I'm testing my code all the functions works except for when you don't put anything in then it gives a error message saying:

Traceback (most recent call last):
File "python", line 6, in
IndexError: string index out of range

The if line is the one I used in earlier exercises and it works for when you put in numbers so what is the problem now?

//

Adam


#2

well, getting the first letter should happen after you validated (if statement) that the user input is valid


#3

Thank you!

I moved the variables down under the if statement.

pyg = 'ay'

original = raw_input('Enter a word:') #question to the user
word = original

if len(original) > 0 and original.isalpha():
print original.lower()
else:
print 'Try again'

moved it down here

first = word[0]
new_word = word[1:] + first + pyg

print new_word

It gives the try again message but still says that something is wrong with the first = word[0] line

Traceback (most recent call last):
File "python", line 14, in
IndexError: string index out of range

Should i change that line somehow?


#4

but your else handles invalid input. You only want to get first letter (and new_word and so on) when the input is valid. In other words, this code should only run when if evaluate to true

so at the end of the code, is also not the right place. Just like converting to lowercase, defining first should happen inside the if clause


#5

Great! I got it! Having the information before the ifand the print command below everything makes it trying to run the command even though there is no input. Of course giving an error. As long as the codes runs in the if the problem won't show.

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

Thanks for the help


#6

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