From the Python.org tutorial:
>>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
first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
Why did it fall through? http://www.python.org/doc/current/tu...00000000000000
Thanks. 7 1742
jmDesktop schrieb:
From the Python.org tutorial:
>>>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
first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
Why did it fall through?
print out what range(2, n) for n == 2 is.
And if you didn't know  2 *IS* a prime.
Diez
On Apr 9, 4:58*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
jmDesktop schrieb:
From the Python.org tutorial:
>>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
first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
Why did it fall through?
print out what range(2, n) for n == 2 is.
And if you didn't know  2 *IS* a prime.
Diez Hide quoted text 
 Show quoted text 
I do not understand.
jmDesktop wrote:
>>From the Python.org tutorial:
>>>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
first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
Why did it fall through?
>>range(2, 2)
[]
>>>
The loop body executes zero times.
regards
Steve

Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
jmDesktop schrieb:
On Apr 9, 4:58 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
>jmDesktop schrieb:
>>From the Python.org tutorial: >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 first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong? Why did it fall through?
print out what range(2, n) for n == 2 is.
And if you didn't know  2 *IS* a prime.
Diez Hide quoted text 
 Show quoted text 
I do not understand.
for <variablein <sequenceloops over a sequence. And of course it
doens't if the sequence is empty, because you can't loop over something
that is empty, can't you?
and range(2,2) is the empty sequence.
Diez
So what is n and x in the first iteration? Sorry. I'm trying.
When n == 2, the inner loop executes 0 times (the length of range(2,n)) and
then falls thru to the else clause, printing the correct answer.
"jmDesktop" <ne***********@gmail.comwrote in message
news:a6**********************************@e39g2000 hsf.googlegroups.com...
So what is n and x in the first iteration? Sorry. I'm trying.
Remember how Python's range operator works. range(n, x) constructs a list
that consists of all elements starting with n and up to, but /not
including/, x. For example, range(3, 7) constructs the list [3, 4, 5, 6].
So what happens if you try to construct the list range(2, 2)? In this case,
Python will construct an empty list  this is its interpretation of "up to,
but not including n" for an argument like range(n, n). This is precisely
what is happening in the first iteration of the first enclosing for loop.
Trace through the code; see that 2 is bound to n in the first iteration; see
that the inner loop then tries to construct the list range(2, n), which is
range(2, 2), which is an empty list. And a "for x in []" statement will not
execute even once.
As it happens, your loop is perfectly correct. You are testing for divisors
for a number excluding 1 and the number itself. For the number 2, the
number of possible divisors satisfying this condition is an empty set. So 2
is, quite correctly, adjudged to be prime.
jmDesktop wrote:
[...]
So what is n and x in the first iteration? Sorry. I'm trying.
Somewhat feebly, if you don't mind my saying so, but don't worry.
The usual way to proceed in the face of such ignorance is to insert some
form of output that will tell you the answer to your question.
So:
>>for n in range(2, 20):
.... print range(2, n)
.... for x in range(2, n):
.... if n % x == 0:
.... print n, 'equals', x, '*', n/x
.... break
.... else:
.... print n, "is prime"
....
[]
2 is prime
[2]
3 is prime
[2, 3]
4 equals 2 * 2
[2, 3, 4]
5 is prime
[2, 3, 4, 5]
6 equals 2 * 3
[2, 3, 4, 5, 6]
7 is prime
[2, 3, 4, 5, 6, 7]
8 equals 2 * 4
[2, 3, 4, 5, 6, 7, 8]
9 equals 3 * 3
and so on! This is the value of the interactive interpreter.
regards
Steve

Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/ This discussion thread is closed Replies have been disabled for this discussion. Similar topics
699 posts
views
Thread by mike420 
last post: by

7 posts
views
Thread by Michael Foord 
last post: by

3 posts
views
Thread by Player 
last post: by

15 posts
views
Thread by Guyon Morée 
last post: by

68 posts
views
Thread by Lad 
last post: by

18 posts
views
Thread by Marko.Cain.23 
last post: by

22 posts
views
Thread by Cesar G. Miguel 
last post: by
          