Reputation: 4555
If I test the "empty" condition of the following, I get an IndexError that states the string index is out of range. Why is that? I want the script to print "empty" if the user input is empty.
pyg = 'ay'
original = raw_input('Enter a word:')
word = original.lower()
first = word[0]
if len(original) > 0 and original.isalpha():
if first in 'aeiou':
print "vowel"
new_word = original + pyg
print new_word
else:
new_word = word[1:len(original)] + first + pyg
print new_word
else:
print "empty"
Upvotes: 0
Views: 234
Reputation: 353229
first = word[0]
is failing; if word
is empty, there is no zeroth character. You can replace this by
first = word[:1]
But since it's only used inside the if len(original) > 0 and original.isalpha():
branch, it'd be better to move it inside instead and leave it as first = word[0]
.
BTW, instead of if len(original) > 0)
, you can simply write if original
-- nonempty strings are truelike.
Upvotes: 1