【问题标题】:Print leap year for next 15 years from given year of input从给定的输入年份打印接下来 15 年的闰年
【发布时间】:2020-09-17 12:38:27
【问题描述】:

我想从作为输入的年份开始打印接下来的 15 个闰年 由于我的代码遵循控制卡在循环中

def find_leap_years(given_year):
    list_of_leap_years=[]
    condition =given_year+15 
    while(given_year<=condition):
        if(given_year%4==0):
            list_of_leap_years.append(given_year)
        else:
            continue    
        given_year=given_year+1 
    print(list_of_leap_years)    
    

    # Write your logic here

    return list_of_leap_years

list_of_leap_years=find_leap_years(2000)
print(list_of_leap_years)

预期输出是从给定值打印下一个未来 15 年的闰年 提前致谢

【问题讨论】:

  • 有什么问题?
  • 您要打印接下来的 15 个闰年还是未来 15 年发生的闰年?你的代码似乎试图做第二个
  • 循环不超过1次迭代未得到预期输出
  • 我想从给定值打印未来 15 年的闰年
  • while 循环在这里使用起来是不必要的复杂。请改用for year in range(given_year, given_year+15):

标签: python loops oop if-statement while-loop


【解决方案1】:

据我了解你的

else:
        continue   

让我们在 2000 年无限循环。摆脱它。然后你得到

[2000, 2004, 2008, 2012]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多