grapeShifter grapeShifter - 10 months ago 69
Python Question

Pig Latin Translator won't give out a word

Hi I need to complete a working Pig Latin translator for a class. The translator is supposed to take a word and if it begins with a vowel add "ay" at the end and if it begins with a consonant take the consonant to the back and then add "ay" it should keep moving back until it hits a vowel. For example the word ¨tree¨ should become "eetray". I´m not sure where in the code I when wrong, but when I run the could it doesn't give me anything so if I put in "tree" it will just restart the code and ask for a word again. Any help would be welcome. Thanks!

vowels = "AEIOU"
realLetters = "abcdefghijklmnopqrstuvwxyz"
while true:
isRealWord = True
word = raw_input ("Please tell me a word to translate: ").lower()
for letter in range(len(word)):
if word[letter] in realLetters:
print "That is not a real word. Just leave."
isRealWord = False

if isRealWord == True:
firstLetter = word[0]
secondLetter = word[1]
thirdLetter = word[2]

pigWord0 = word + "ay"

TheRestOfTheWord = word[1:]
pigWord1 = TheRestOfTheWord + firstLetter + "ay"

TheRestOfTheWord = word[2:]
pigWord2 = TheRestOfTheWord + firstLetter + secondLetter + "ay"

TheRestOfTheWord = word[3:]
pigWord3 = TheRestOfTheWord + firstLetter + secondLetter + thirdLetter + "ay"

if pigWord0[0] in vowels:
print pigWord0.lower()
elif pigWord1[0] in vowels:
print pigWord1.lower()
elif pigWord2[0] in vowels:
print pigWord2.lower()
elif pigWord3[0] in vowels:
print pigWord3.lower()

Answer Source

I suspect the indentation/syntax errors are issues with SO and/or lazy copying, as I can replicate your failure example with fixed code:

Everything is compared lowercase except the vowels list, which is in all caps. it needs to be aeiou, as A != a and everything else is lowercase.

You still have to fix your syntax, but this solves the question as asked.