在Python教程中使用break命令时出现问题

发布于 2024-08-09 03:18:52 字数 1117 浏览 8 评论 0原文

我正在关注他们网站上的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

从﹋此江山别 2024-08-16 03:18:52

您显示的输出包含字符串“x is a prime number”的倍。但是,该字符串会打印在内循环的 else 子句中,并且每次执行内循环时最多会执行一次。

由于外循环执行八次迭代,因此“x is a prime number”不能打印超过次。因此,您显示的输出不能是由显示的代码产生的。

结论:有些可疑。您可以显示执行时的代码吗?


编辑:解决了!

您错误地缩进了 else 子句,导致 Python 将其解释为属于 if 语句。 Python 将制表符视为 8 个空格。也许您的编辑器将制表符显示为 4 个空格。这样你可能就错过了这个错误。根据 PEP 8,请不要混合使用制表符和空格,并且最好使用四个空格来缩进代码。

>>> 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'
... 
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

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.

>>> 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'
... 
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
℉絮湮 2024-08-16 03:18:52

我最好的猜测是您的“else:”语句没有正确缩进,然后您的结果是合乎逻辑的,请检查您的 else 缩进是否与“for x”处于同一水平。

即您使用:

for n in range(2,10):
    for x in range(2,n):
        if n%x == 0:
            print(n, '=', x, '*', n/x)
            break
        else:
            print(n, 'is a prime')

而不是:

for n in range(2,10):
    for x in range(2,n):
        if n%x == 0:
            print(n, '=', x, '*', n/x)
            break
    else:
        print(n, 'is a prime')

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:

for n in range(2,10):
    for x in range(2,n):
        if n%x == 0:
            print(n, '=', x, '*', n/x)
            break
        else:
            print(n, 'is a prime')

instead of:

for n in range(2,10):
    for x in range(2,n):
        if n%x == 0:
            print(n, '=', x, '*', n/x)
            break
    else:
        print(n, 'is a prime')
爱冒险 2024-08-16 03:18:52

您可能需要更新您的 Python 解释器。

这段代码对我来说运行正确(注意Python版本号):

Python 2.6.1 (r261:67515, Dec  6 2008, 16:42:21) 
[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 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

You may need to update your Python interpreter.

This code runs correctly for me (notice Python version number):

Python 2.6.1 (r261:67515, Dec  6 2008, 16:42:21) 
[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 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
红尘作伴 2024-08-16 03:18:52

我认为你的缩进错误。如果我获取您的代码,并缩进 else 使其位于 if 语句下方,我将得到您所得到的准确输出。

下面的代码重现了您的输出,

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'

同时

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'

执行您想要它执行的操作。

请注意 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,

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'

while

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'

Does what you want it to do.

Note the different in the indenting of the else.

上课铃就是安魂曲 2024-08-16 03:18:52

我认为 else 应该始终与 if 保持一致。这就是我读到的。但在这个素数生成器代码中,一次性写入素数的唯一方法是将 elsefor x 对齐。所以我对这样的认定没有任何解释。虽然我刚刚开始学习Python。

I thought that else should always be aligned with if. That is what I have read. But in this prime number generator code, the only way to get the primes written once, is aligning else with for x. So I have no explanation for such identation. Although I am just starting to learn Python.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文