python – 检查变量的多个值

参见英文答案 >
How to test multiple variables against a value?                                    20个

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    word = original.lower()
    first = str(word)[0]
    print first
    if str(first) == "a" or "e" or "i" or "u" or "o":
        print "vowel"
else:
    print "consonant"

我想检查一个单词是否以元音或辅音开头.但是,这部分不起作用:如果str(first)==“a”或“e”或“i”或“u”或“o”

那么你如何检查第一个字母是“a”还是“e”或“i”或“u”还是“o”?

最佳答案 你最好用

    if len(original) and original.isalpha():
        word = original.lower()
        first = word[0]
        print first
        if first in ('a','e','i','o','u'):
            print "vowel"
        else:
            print "consonant"

你也做错了,如果你试图使用OR子句你必须使用这样但是它不是更好的pythonic方式:

 if first =='a' or first =='e' or first =='i' or first =='o' or first =='u':
点赞