【发布时间】:2018-10-08 17:02:29
【问题描述】:
我刚刚阅读了一堆关于如何在 Python 中处理 StopIteration 错误的帖子,我在解决我的特定示例时遇到了麻烦。我只想用我的代码打印出 1 到 20,但它会打印出错误 StopIteration。我的代码是:(我是这里的新手,所以请不要阻止我。)
def simpleGeneratorFun(n):
while n<20:
yield (n)
n=n+1
# return [1,2,3]
x = simpleGeneratorFun(1)
while x.__next__() <20:
print(x.__next__())
if x.__next__()==10:
break
【问题讨论】:
标签: python while-loop conditional-statements generator break