鱼C论坛

 找回密码
 立即注册
查看: 921|回复: 6

[已解决]求助迭代器输出闰年的课后作业

[复制链接]
发表于 2020-3-7 11:32:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. class LeapYear:
  2.     def __init__(self):
  3.         self.year = 0
  4.         self.temp = 0
  5.       
  6.     def __iter__(self):
  7.         return self
  8.     def __next__(self):
  9.         self.temp  += 1            
  10.         if (self.temp % 4 == 0 and  self.temp % 100 != 0) or self.temp % 400 == 0:
  11.             self.year = self.temp
  12.         else:
  13.             pass
  14.         return self.year
  15.         
  16. leapyear = LeapYear()
  17. for each in leapyear:
  18.      if each >=2000 and each <= 2021:         
  19.          print(each)
复制代码


题目是输出2000年到2021年的所有闰年,但我的代码输出后每个闰年都输出了四次,求大神解答!谢谢!
最佳答案
2020-3-7 11:48:55
  1. class LeapYear:
  2.     def __init__(self):
  3.         self.year = 0
  4.         self.temp = 0
  5.       
  6.     def __iter__(self):
  7.         return self
  8.     def __next__(self):
  9.         self.temp  += 1            
  10.         if (self.temp % 4 == 0 and  self.temp % 100 != 0) or self.temp % 400 == 0:
  11.             self.year = self.temp   
  12.             return self.year
  13.         
  14. leapyear = LeapYear()
  15. for each in leapyear:
  16.     if each is not None:
  17.         if each >=2000 and each <= 2021:         
  18.             print(each)
  19.             if each == 2020:
  20.                 break
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-7 11:35:31 | 显示全部楼层
  1. class LeapYear:
  2.     def __init__(self):
  3.         self.year = 0
  4.         self.temp = 0

  5.     def __iter__(self):
  6.         return self

  7.     def __next__(self):
  8.         self.temp += 1
  9.         if (self.temp % 4 == 0 and self.temp % 100 != 0) or self.temp % 400 == 0:
  10.             self.year = self.temp
  11.             return self.year


  12. leapyear = LeapYear()
  13. for each in leapyear:
  14.     if each is not None:
  15.         if 2000 <= each <= 2021:
  16.             print(each)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-7 11:48:55 | 显示全部楼层    本楼为最佳答案   
  1. class LeapYear:
  2.     def __init__(self):
  3.         self.year = 0
  4.         self.temp = 0
  5.       
  6.     def __iter__(self):
  7.         return self
  8.     def __next__(self):
  9.         self.temp  += 1            
  10.         if (self.temp % 4 == 0 and  self.temp % 100 != 0) or self.temp % 400 == 0:
  11.             self.year = self.temp   
  12.             return self.year
  13.         
  14. leapyear = LeapYear()
  15. for each in leapyear:
  16.     if each is not None:
  17.         if each >=2000 and each <= 2021:         
  18.             print(each)
  19.             if each == 2020:
  20.                 break
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-3-7 11:56:37 | 显示全部楼层
还可以写成
  1. class LeapYear:
  2.     def __init__(self):
  3.         self.year = 0
  4.       
  5.     def __iter__(self):
  6.         return self
  7.     def __next__(self):
  8.         while True:
  9.             self.year += 1
  10.             if (self.year % 4 == 0 and  self.year % 100 != 0) or self.year % 400 == 0:
  11.                 return self.year
  12.         
  13. leapyear = LeapYear()
  14. for each in leapyear:
  15.      if each >=2000 and each <= 2021:         
  16.          print(each)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-9 14:32:54 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-9 14:33:46 | 显示全部楼层

谢谢老哥
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-9 14:34:20 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-2 09:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表