In the python tutorial is an example (copied below), shouldn’t else be indented? I ran the code and it didn’t work but I indented it (else) and it worked. Is, what I am saying right? If the documentation is wrong, then how do I report it as a bug to python doc guys?
>>> 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'
...
Tha example is working and the indented is fine, have a look here:
As you can see, the
elserelates to the secondforby following this rule:In the example, it means that the else will be called if the second for (in the second line) will finish running but will never run the break command – only if
n % x == 0never eval toTRUE.If
(n % x == 0)at any point the break will be called the second for will stop,nfrom the first for will grow by 1, (n = n + 1) and the second for will be called again with a newn.