我想编写一个函数,测试每个元音是否出现在其参数中,如果文本包含任何小写元音,则返回False,否则返回True
我的代码如下:
def hasNoVowel(text):
return ('a' not in text) or ('u' not in text) or ('o' not in text) or ('i' not in text) or ('e' not in text)
print(hasNoVowel('it is a rainy day'))
print(hasNoVowel('where is the sun?'))
print(hasNoVowel("rhythm"))
然而,我得到的结果是:
True
True
True
代替:假,假,真
有人能帮我解释一下我做错了什么吗
提前谢谢你
您需要在函数中使用
and
而不是or
。当前,仅当所有五个元音都存在时,函数才会返回False
:您可以使用
any(...)
评估条件并缩短代码:输出:
相关问题 更多 >
编程相关推荐