我是靠谱客的博主 糊涂红牛,最近开发中收集的这篇文章主要介绍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:找出元组是否包含偶数的函数?所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部