Code works but is using the previous word


On PygLatin, I got to the end with no problems. The code does work-ish. But the first time I tried it, I tried the word bird. It said error. A little tweak and then I tried using dog. In this case it properly formatted the word bird. I typed in dog and it gave me irdbay (for bird) instead of ogday (for dog). And it has done that with all the words I have tried, it gives me the previous word. I got pass it by trying the same word twice, but I want to know if it was just a glitch or if it was an error with the code?

print "Welcome to the Pig Latin Translator "
pyg = 'ay'
new_word = word[1:len(new_word)] + first + pyg

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
  word = original.lower()
  first = word[0]
    print 'empty'
print new_word


this code shouldn’t even be able to run, it seems your browser is sending cached data. If i run your code here:

it throws an error, as it should. You reference word, new_word and first before you have named/defined this variables.

The order of your code is a bit off. its the right code, but in the wrong order/some lines of code are not at the right place, so the flow of your program is wrong


Thanks, I moved a line at the top to the second from the bottom and it works now.


yes, but do you really want to do this:

new_word = word[1:len(new_word)] + first + pyg
print new_word

if the user enters an invalid word? No, so these two lines should be in the if clause

code just working isn’t enough in today’s industry, the program has to be logic and well designed as well



Ok, that makes sense. I have only been learning about this stuff for a day. Does the above look right, it seems to be working correctly on that website, even when I use an invalid word.


Perfection, nicely done :slight_smile:


Thanks!! You seem to know a lot, I was wonder if you could tell me how you go about doing more than one word. Right now it just lets you do the one and then you have to press run again. Is there another way to have it reset. I did add code to do more words, by duplicating it, is that the only way? The additional code looks like this

not that I really love pig latin, but I think the concept can be applied in other ways.


what you currently do violates the DRY (don’t repeat yourself) principal, very fundamental principal of programming

we could simply ask the user to enter a sentence:

original = raw_input("enter a sentence: ")

then we can split the string into a list:

words = original.split()

then we need to verify all the words in the list are valid:

is_valid = True
for word in words:
   if not word.isalpha():
      is_valid = False

but it seems i already introduce a concept (loops), which is new for you

maybe progress further before taking on this challenge?


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