在Python教程中使用break命令时出现问题
我正在关注他们网站上的 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'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
在我看来,它继续运行内部 for 循环,而不是吐出上面所说的内容
3 is a prime number
4 equals 2 * 2
5 is a prime number
5 is a prime number
5 is a prime number
6 equals 2 * 3
7 is a prime number
7 is a prime number
7 is a prime number
7 is a prime number
7 is a prime number
8 equals 2 * 4
9 is a prime number
9 equals 3 * 3
,但为什么教程不考虑这一点?对于最新的解释器版本来说它是否已经过时了(我正在运行 xubuntu jaunty)?
我能够通过添加该行来修复它
else:
... if n != y:
... print n, 'is a prime number'
... y = n
,但我担心这可能是不好的编码实践。感谢您的帮助。
I'm following the python tutorial at their site and I'm currently at the break continue section. I just tried this sample code.
>>> 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'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
And instead of spitting out what it says above I get
3 is a prime number
4 equals 2 * 2
5 is a prime number
5 is a prime number
5 is a prime number
6 equals 2 * 3
7 is a prime number
7 is a prime number
7 is a prime number
7 is a prime number
7 is a prime number
8 equals 2 * 4
9 is a prime number
9 equals 3 * 3
It seems to me that it continues to run the inside for loop, but why would the tutorial not take this into account? Is it outdated for the latest interpreter build (I'm running xubuntu jaunty)?
I was able to fix it by adding the line
else:
... if n != y:
... print n, 'is a prime number'
... y = n
but I am concerned this may be bad coding practice. Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您显示的输出包含字符串“x is a prime number”的十倍。但是,该字符串会打印在内循环的 else 子句中,并且每次执行内循环时最多会执行一次。
由于外循环执行八次迭代,因此“x is a prime number”不能打印超过八次。因此,您显示的输出不能是由显示的代码产生的。
结论:有些可疑。您可以显示执行时的代码吗?
编辑:解决了!
您错误地缩进了 else 子句,导致 Python 将其解释为属于
if
语句。 Python 将制表符视为 8 个空格。也许您的编辑器将制表符显示为 4 个空格。这样你可能就错过了这个错误。根据 PEP 8,请不要混合使用制表符和空格,并且最好使用四个空格来缩进代码。The output which you show contains ten times the string "x is a prime number". However, this string is printed in the
else
clause of the inner loop, and is as such executed at most once for each execution of the inner loop.Since the outer loop performs eight iterations, "x is a prime number" cannot be printed more than eight times. Thus the output which you display cannot be brought about by the code shown.
Conclusion: something is fishy. Can you show the code as you executed it?
Edit: solved!
You incorrectly indented the else clause, such that Python interpreted it as belonging to the
if
statement. Python treats a tab as 8 spaces. Perhaps your editor displays tabs as 4 spaces. That way you may have missed this bug. As per PEP 8, please don't mix tabs and spaces, and preferably use four spaces to indent your code.我最好的猜测是您的“else:”语句没有正确缩进,然后您的结果是合乎逻辑的,请检查您的 else 缩进是否与“for x”处于同一水平。
即您使用:
而不是:
My best guess is that your 'else:' statement isn't properly indented, and then your result is logical, check that you else's indentation is on level with 'for x'.
I.e. you use:
instead of:
您可能需要更新您的 Python 解释器。
这段代码对我来说运行正确(注意Python版本号):
You may need to update your Python interpreter.
This code runs correctly for me (notice Python version number):
我认为你的缩进错误。如果我获取您的代码,并缩进 else 使其位于 if 语句下方,我将得到您所得到的准确输出。
下面的代码重现了您的输出,
同时
执行您想要它执行的操作。
请注意 else 缩进的不同之处。
I think you have got indents wrong. If I take your code, and indent the else so that it is under the if statement, I get exactly the output that you are getting.
The code below reproduces your output,
while
Does what you want it to do.
Note the different in the indenting of the else.
我认为
else
应该始终与if
保持一致。这就是我读到的。但在这个素数生成器代码中,一次性写入素数的唯一方法是将else
与for x
对齐。所以我对这样的认定没有任何解释。虽然我刚刚开始学习Python。I thought that
else
should always be aligned withif
. That is what I have read. But in this prime number generator code, the only way to get the primes written once, is aligningelse
withfor x
. So I have no explanation for such identation. Although I am just starting to learn Python.