我是靠谱客的博主 糊涂红牛,这篇文章主要介绍python找出0到20内偶数,Python:找出元组是否包含偶数的函数?,现在分享给大家,希望可以做个参考。

So what I want is a function that gets a tuple input from a user and then figures out if that tuple contains an even number. I want to know how to do it with a for or while loop. I've tried it but the code doesn't work. I've kind of got something but it's not working:

def ifEven(x):

i = -1

if isinstance(x, tuple):

while i < len(x):

for i in x:

i = i + 1

if x[i] % 2 == 0:

return True

else:

return False

解决方案

Here is a working code:

def ifEven(x):

if isinstance(x, tuple):

for i in x:

if i % 2 == 0:

return True

return False

That being said, it can be rewritten as a one-liner using Python's generator expressions:

def isEven(x):

return any(v % 2 == 0 for v in x)

最后

以上就是糊涂红牛最近收集整理的关于python找出0到20内偶数,Python:找出元组是否包含偶数的函数?的全部内容,更多相关python找出0到20内偶数,Python:找出元组是否包含偶数内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(131)

评论列表共有 0 条评论

立即
投稿
返回
顶部