Bug with PygLatin first=word[0]


#1

I have already finished this lesson, but I would like to fix a bug.

https://www.codecademy.com/courses/python-beginner-2W5v7/0/1?curriculum_id=4f89dab3d788890003000096

If I leave empty when the software ask me a word this error appear:

Traceback (most recent call last):
  File "D:/Desktop/Francesco/Python/boh.py", line 5, in <module>
    first = word[0]
IndexError: string index out of range

instead of "Write an english word".

I try to change the code like this

if len(word) == 0:
    print "Write an english word
elif ....etc

but doesn't work

This is the original code.

print 'Welcome to the Pig Latin Translator!'
pyg = 'ay'
original = raw_input("Enter a word:")
word = original.lower()
first = word[0]
new_word = word[1:len(word)] + first + pyg



if len(original) > 0 and original.isalpha():
    print new_word
elif original.isalnum():
    print "No numbers allowed"
else:
    print "Write an English word"

How can I fix this?


#2

here:

if len(original) &gt; 0 and original.isalpha():

you validate your input. So if you place the logic of your program (converting to lowercase, getting first letter and so on) inside your if clause, you validate the user input before manipulating it


#3

And how can I do it?
I tried to insert a code after

original = raw_input("Enter a word:")
if len(original) <= 0:
    print "Write something"

and it works, it prints "Write something", but after that show the same error...

Welcome to the Pig Latin Translator!
Enter a word:
Write something

Traceback (most recent call last):
  File "D:/Desktop/Francesco/Python/boh.py", line 8, in <module>
    first = word[0]
IndexError: string index out of range

Maybe I have to use a "loop" for return to "raw_input" but I dont know how :disappointed_relieved: is that right?
Maybe I have to continue learning before programming something, lol


#4

but even in the lessons the following order is handles: (this is not actual code, just to give you an idea of the flow of your program)

pyg = 'ay'
# prompt the user for input
raw_input
# validate the user input is valid
if condition:
    # convert to lowercase
    lower
    # get first letter
    [0]
    # rest of the translation
else:
   # the user input wasn't valid, let them know

see how i place the manipulation of the user string after the validation?


#5

Aww I get it (I think)! lol
Now the code works!

print 'Welcome to the Pig Latin Translator!'
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(word)] + first + pyg
    print new_word
elif original.isalnum():
    print "No numbers allowed"
else:
    print "Write an English word"

It is right?

I didn't understand that "if" could include variables :stuck_out_tongue:
Thanks a lot!


#6

You did it right :slight_smile:

Yea, if can include variable (thankfully)


#7

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