小编Bar*_*sen的帖子

如何解决Python中的StopIteration错误?

我刚刚读了一堆关于如何处理 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
Run Code Online (Sandbox Code Playgroud)

python generator break while-loop conditional-statements

6
推荐指数
1
解决办法
3万
查看次数