gpt4 book ai didi

python - 不应该在下面的代码中缩进

转载 作者:行者123 更新时间:2023-11-28 19:37:43 26 4
gpt4 key购买 nike

python tutorial是一个例子(复制在下面),难道else不应该缩进吗?我运行了代码,它没有工作,但我缩进了它 (else),它工作了。我说的对吗?如果文档有误,我该如何将其作为错误报告给 python 文档人员?

>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... # loop fell through without finding a factor
... print n, 'is a prime number'
...

最佳答案

查看您链接的文档:

Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement. This is exemplified by the following loop, which searches for prime numbers:

关于python - 不应该在下面的代码中缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12194947/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com