0

I'm trying to make a Pig Latin translator, but ran into some trouble when testing if the first letter is a vowel. I did some googling and found a way to solve it, but I wanted to know why one work and the other doesn't. Here is my code (the commented section is the part that doesn't work):

word = raw_input("Input a word: ").lower()
vowels = {'a', 'e', 'i', 'o', 'u'}

if len(word) > 0 and type(word) == str:
    #if word[0] == ('a' or 'e' or 'i' or 'o' or 'u'):
    #   pigWord = word + 'ay'
    #   print pigWord

    if word[0] in vowels:
        pigWord = word + 'ay'
        print pigWord
    else:
        pigWord = word + word[0] + 'ay'
        print pigWord[1:]
else: 
    print 'Please enter a valid word.'

It seems that when I use the first method (the commemted section) it only tests if the first letter is an "a", if not it goes to "else:".

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
eirik-ff
  • 153
  • 9
  • This question appears to be off-topic because it is a simple question that a professional or enthusiast programmer would not ask without reading the documentation first. –  Jun 09 '14 at 18:21
  • This isn't really the same situation as the proposed duplicate question. In that question the statement is always evaluating the same, while here it evaluates differently based on what is being compared. – mclaassen Jun 09 '14 at 18:51

1 Answers1

0

Its because the statement:

('a' or 'e' or 'i' or 'o' or 'u')

is being evaluated first which is essentially just returning the first thing in that list which is true. Since the first thing in the list 'a' evaluates to true it is returned, and then your comparison is really saying word[0] == 'a'

mclaassen
  • 5,018
  • 4
  • 30
  • 52