鱼C论坛

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

[已解决]48讲迭代器问题

[复制链接]
发表于 2020-2-15 20:49:34 | 显示全部楼层 |阅读模式

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

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

x

  1. class LeapYear:
  2.     def __init__(self):
  3.         self.n = 0
  4.         
  5.     def __iter__(self):
  6.         return self

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


  11. year = LeapYear()

  12. for i in year:
  13.         if i > 1999:
  14.                 print(i)
  15.         else:
  16.                 break
复制代码

报错:if i > 1999:
TypeError: '>' not supported between instances of 'NoneType' and 'int'

请问next()方法不是返回整形?哪里写错了?
最佳答案
2020-2-15 20:57:39
redforce 发表于 2020-2-15 20:57
if i is not None  
为啥要判断不是None,整个过程中i好像没有为空的时候啊
  1.     def __next__(self):
  2.         self.n += 1
  3.         if ((self.n%4 == 0 and self.n%100 != 0) or (self.n%400 == 0)):
  4.             return self.n
复制代码


当 self.n 不符合条件时没有返回值
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-15 20:51:44 | 显示全部楼层
这样:

  1. class LeapYear:
  2.     def __init__(self):
  3.         self.n = 0

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

  6.     def __next__(self):
  7.         self.n += 1
  8.         if ((self.n % 4 == 0 and self.n % 100 != 0) or (self.n % 400 == 0)):
  9.             return self.n


  10. year = LeapYear()

  11. for i in year:
  12.     if i is not None:
  13.         if i < 1999:
  14.             print(i)
  15.         else:
  16.             break
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-15 20:57:13 | 显示全部楼层

if i is not None  
为啥要判断不是None,整个过程中i好像没有为空的时候啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-15 20:57:39 | 显示全部楼层    本楼为最佳答案   
redforce 发表于 2020-2-15 20:57
if i is not None  
为啥要判断不是None,整个过程中i好像没有为空的时候啊
  1.     def __next__(self):
  2.         self.n += 1
  3.         if ((self.n%4 == 0 and self.n%100 != 0) or (self.n%400 == 0)):
  4.             return self.n
复制代码


当 self.n 不符合条件时没有返回值
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-15 21:00:41 | 显示全部楼层
哦,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-15 21:11:25 | 显示全部楼层
也可以这样
  1. class LeapYear:
  2.     def __init__(self):
  3.         self.n = 0

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

  6.     def __next__(self):
  7.         while True:
  8.             self.n += 1
  9.             if ((self.n % 4 == 0 and self.n % 100 != 0) or (self.n % 400 == 0)):
  10.                 return self.n


  11. year = LeapYear()

  12. for i in year:
  13.     if i < 1999:
  14.         print(i)
  15.     else:
  16.         break
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-16 18:28:18 | 显示全部楼层

嗯,了解,谢谢,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-3 03:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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