检查变量的多个值 [英] Checking multiple values for a variable
本文介绍了检查变量的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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"
我想检查一个单词是否以元音或辅音开头.但是,这部分不起作用:if str(first) == "a" or "e" or "i" or "u" or "o"
I want to check if a word starts with a vowel or consonant. However, this part does not work: if str(first) == "a" or "e" or "i" or "u" or "o"
那么你将如何检查第一个字母是a"或e"还是i"或u"或o"?
So how would you check if the first letter is either "a" or "e" or "i" or "u" or "o"?
推荐答案
你最好使用 in
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 方式:
also you are doing it wrong, if you are trying to use OR clause you must use like this BUT it's not the better pythonic way:
if first =='a' or first =='e' or first =='i' or first =='o' or first =='u':
这篇关于检查变量的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文