【问题标题】:Python: find the first even number in a list using a while loopPython:使用while循环查找列表中的第一个偶数
【发布时间】:2016-07-01 23:01:48
【问题描述】:

我正在尝试使用 while 循环编写一个返回列表中第一个偶数的函数

def first_even(list):
    while i%2 !=0:   #I'm not sure if this is a good start for the while loop, but I think I'm supposed to use something%2...
        #no idea what to do here

【问题讨论】:

  • 使用Div功能! link

标签: python-3.x while-loop


【解决方案1】:

一个好的和非常短的编码风格是:

  for i in list:
     if i%2 == 0:
        return i

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 2016-04-12
    • 2018-07-06
    • 2022-12-23
    • 2021-11-13
    • 1970-01-01
    • 2013-10-30
    相关资源
    最近更新 更多