444,001 Members | 1,262 Online Need help? Post your question and get tips & solutions from a community of 444,001 IT Pros & Developers. It's quick & easy.

# basic python question about for loop

 P: n/a 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. Apr 9 '08 #1
7 Replies

 P: n/a 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 Apr 9 '08 #2

 P: n/a On Apr 9, 4:58*pm, "Diez B. Roggisch" >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. Apr 9 '08 #3

 P: n/a 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/ Apr 9 '08 #4

 P: n/a jmDesktop schrieb: On Apr 9, 4:58 pm, "Diez B. Roggisch" 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 number3 is a prime number4 equals 2 * 25 is a prime number6 equals 2 * 37 is a prime number8 equals 2 * 49 equals 3 * 3first 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

 P: n/a |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. Apr 9 '08 #6

 P: n/a "jmDesktop"

 P: n/a 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  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/ Jun 27 '08 #8

### This discussion thread is closed

Replies have been disabled for this discussion. 