Frage

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
War es hilfreich?

Lösung

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':
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top