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.