为什么我的python pig latin程序每次输出word + way?

时间:2014-04-29 22:40:49

标签: python logic semantics python-3.4

我正在尝试使用Python制作Pig Latin翻译器。如果单词以元音开头,则应附加“way”。如果单词以辅音开头,则单词,第一个字母移到最后,并且附加的“ay”应该打印出来。 猫应该打印atcay,Apple应该打印appleway。然而,两者最终都附加到最后。我解析了,似乎无法找到错误。我认为它与elif语句有关,也许它已经停在那里,但我是编程的新手,我不确定。

我做错了什么?

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

1 个答案:

答案 0 :(得分:1)

此行不正确,因为它始终会评估为true:

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

你打算写的是:

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

您可以通过以下方式简化:

elif word[0] in 'aeiou':