Problem using break command in Python tutorial

I am following the python tutorial on my site and I am currently in the 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 he 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 looks to me like it keeps running the for loop, but why doesn't the tutorial take that into account? Is it deprecated for the latest version of the interpreter (I'm running xubuntu jaunty)?

I managed to fix this by adding the line

     else:
...                     if n != y:
...                             print n, 'is a prime number'
...                             y = n

      

but I am concerned that this might be wrong coding practice. Thank you for your help.

+2


source to share


5 answers


The output you are showing is ten when the string "x is prime". However, this line is printed in the statement of the else

inner loop and as such is executed at most once for each execution of the inner loop.

Since the outer loop performs eight iterations, "x is prime" cannot be printed more than eight times. Thus, the output you are showing cannot appear in the shown code.

Conclusion : something fishy. Can you show the code while executing it?




Edit: Allowed!

You separated the else clause incorrectly, so that Python interpreted it as belonging to a statement if

. Python treats a tab as 8 spaces. Perhaps your editor displays tabs as 4 spaces. Thus, you may have missed this error. As of PEP 8 , please do not mix tabs and spaces, and preferably use four spaces for code indentation.

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

      

+4


source


My best guess is that your "else:" statement is not properly indented, in which case your result is logical, check that you are still indented one more level with "for x".

those. you're using:

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:

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

      

+1


source


You may need to update your Python interpreter.

This code works correctly for me (note the 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

      

0


source


I think you have the wrong indentation. If I take your code and indent the other so that it sits under the if statement, I get exactly the result that you get.

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'

      

and

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.

Note the difference in else indentation.

0


source


I thought I else

should always be aligned with if

. This is what I read. But in this prime number generator code, the only way to get primes written once is align else

with for x

. Therefore, I have no explanation for this identification. Although I am just starting to learn Python.

0


source







All Articles