basic python question about for loop
Steve Holden
steve at holdenweb.com
Sun Apr 13 00:16:30 CEST 2008
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/
More information about the Python-list
mailing list