0

I am attempting to make a Pig Latin translator using Python. If the word begins with a vowel, "way" should be appended. If the word begins with a consonant, the word, with the first letter moved to the end, and "ay" appended should print. Cat should print atcay, and Apple should print appleway. However, both end up having way appended to the end. I parsed through and cannot seem to find the error. I am thinking it has to do with the elif statement, maybe it stopped there, but I am new to programming and am not sure.

What am I doing wrong?

print('Welcome to the Pig Latin translator!')

pyg = 'ay'
word = input('Enter a word: ')
word = word.lower()

if word.isalpha() == False:                         #  Checks if string is empty and consists of only letters. 
    print('It seems that you did not enter a word, try again. ')
elif word[0] == 'a' or 'e' or 'i' or 'o' or 'u':    #  If first letter is a vowel
        print(str(word) + 'way')                    #  print variable word plus the string way
else:
    newword = word + word[0] + pyg                  #  If first letter is consonant(not vowel) and  consists of only letters
    print(newword[1:])                              #  print the word characters 1(not 0) through last
  • possible duplicate of [Elif and if not working or me not understanding](http://stackoverflow.com/questions/14636446/elif-and-if-not-working-or-me-not-understanding) – Brendan Long Apr 29 '14 at 22:46
  • or: http://stackoverflow.com/questions/15112125/if-x-or-y-or-z-blah – Brendan Long Apr 29 '14 at 22:47
  • 2
    Oh wow, here's a duplicate where someone else is trying to make a pig latin translator. http://stackoverflow.com/questions/13305089/issues-with-a-if-else-loop-in-python?rq=1 – Brendan Long Apr 29 '14 at 22:47
  • Haha, great point @BrendanLong, except that the linked pig latin translator would return 'appleay' not 'appleway'. :-/ – exp1orer Apr 29 '14 at 22:57

1 Answers1

1

This line is incorrect, as it will always evaluate to true:

elif word[0] == 'a' or 'e' or 'i' or 'o' or 'u':

What you meant to write was:

elif word[0] == 'a' or word[0] == 'e' or word[0] == 'i' or word[0] == 'o' or word[0] == 'u':

You can simplify it by doing:

elif word[0] in 'aeiou':
savanto
  • 4,470
  • 23
  • 40
  • 1
    To expand on the first line of this answer, it's because non-empty strings will evaluate to `True` and Boolean logic (and, or) doesn't carry over the operator ( ==, >,etc) – exp1orer Apr 29 '14 at 22:55