无法从辅音中确定元音

Win*_*neh 2 python character

使用下面的代码,无论输入的第一个字母是什么,它总是被确定为元音:

original = raw_input("Please type in a word: ")
firstLetter = original[0]
print firstLetter

if firstLetter == "a" or "e" or "i" or "o" or "u":
    print "vowel"
else:
    print "consonant"
Run Code Online (Sandbox Code Playgroud)

实际上,在if语句中布尔值是什么并不重要......如果它是==或!=,它仍然是返回"vowel".为什么?

Sta*_*ven 8

Python不是英语.如果你在它们之间orand之间有一堆表达式,那么每个表达式都必须有意义.请注意,它本身:

if "e":
    print("something")
Run Code Online (Sandbox Code Playgroud)

将永远打印something,即使letter不相等"e".

你需要这样做:

if letter == "a" or letter == "e"  # (...)
Run Code Online (Sandbox Code Playgroud)

或者,更简洁:

if letter in "aeiouy":
Run Code Online (Sandbox Code Playgroud)