2

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.

4

5 に答える 5

4

表示する出力には、文字列「 xは素数」の10倍が含まれています。ただし、この文字列は内部ループの句に出力されるため、内部ループの実行ごとに最大1回実行されます。else

外側のループは8回の反復を実行するため、「xは素数です」を8回以上印刷することはできません。したがって、表示する出力は、表示されているコードでは実現できません。

結論:何かが怪しい。実行時にコードを表示できますか?


編集:解決しました!

ifPythonがそれをステートメントに属していると解釈するように、else句を誤ってインデントしました。Pythonはタブを8つのスペースとして扱います。おそらく、エディタはタブを4つのスペースとして表示します。そうすれば、このバグを見逃している可能性があります。PEP 8に従い、タブとスペースを混在させないでください。できれば、コードをインデントするために4つのスペースを使用してください。

>>> 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
于 2009-10-26T14:46:06.350 に答える
1

私の推測では、「else:」ステートメントが適切にインデントされておらず、結果が論理的です。他の人のインデントが「forx」と同じレベルにあることを確認してください。

つまり、使用します:

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')
于 2009-10-26T14:50:28.290 に答える
0

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
于 2009-10-26T14:43:23.157 に答える
0

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.

于 2009-10-26T14:56:28.170 に答える
0

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.

于 2011-03-16T17:09:29.597 に答える