zoukankan      html  css  js  c++  java
  • 迭代器求迄今为止所有的闰年

     1 #写一个迭代器,要求输出迄今为止所有的闰年
     2 import datetime as dt
     3 
     4 class LeapYear:
     5     def __init__(self):
     6         self.this_year = dt.date.today().year
     7 
     8 
     9     def isLeapYear(self,year):
    10         if (year % 4 == 0 and year % 100 != 0)
    11            or (year % 100 == 0 and year % 400 ==0):
    12             return True
    13         else:
    14             return False
    15 
    16 
    17     def __iter__(self):
    18         return self
    19 
    20     def __next__(self):
    21         while not self.isLeapYear(self.this_year):
    22             self.this_year -= 1
    23 
    24         temp = self.this_year
    25 
    26         self.this_year -= 1
    27 
    28         return temp
    29 
    30 
    31 leapyear = LeapYear()
    32 for i in leapyear:
    33     if i >= 1900:
    34         print(i)
    35     else:
    36         break
  • 相关阅读:
    c语言之数据类型
    C语言之概述
    012.day12
    011.day011
    010.day010
    010.day08
    010.周六自习
    009.day07
    008.day06
    007.day05
  • 原文地址:https://www.cnblogs.com/themost/p/6545237.html
Copyright © 2011-2022 走看看