By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,905 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,905 IT Pros & Developers. It's quick & easy.

Problem with following python code

P: n/a
I've been having problem with the following code. It's supposed to
print the prime numbers between 10 and 100. But i'm not getting any
output, i.e. i guess the outer 'for' loop is being traversed only
once. I would be greatful if you could help me out. Thanx!
>>f=1
for i in range(10,100):
.... for j in range(2,i):
.... if i%j==0:
.... f=0
.... break
.... else: continue
.... if f==1:
.... print i,
....

Jun 12 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On 6/12/07, why? <ji*******@yahoo.comwrote:
I've been having problem with the following code. It's supposed to
print the prime numbers between 10 and 100. But i'm not getting any
output, i.e. i guess the outer 'for' loop is being traversed only
once. I would be greatful if you could help me out. Thanx!
>f=1
for i in range(10,100):
You need to switch these two lines to reset the flag each time around
the outer loop.

Cheers,

Tim
... for j in range(2,i):
... if i%j==0:
... f=0
... break
... else: continue
... if f==1:
... print i,
...

--
http://mail.python.org/mailman/listinfo/python-list
Jun 12 '07 #2

P: n/a
En Tue, 12 Jun 2007 01:25:31 -0300, why? <ji*******@yahoo.comescribió:
I've been having problem with the following code. It's supposed to
print the prime numbers between 10 and 100. But i'm not getting any
output, i.e. i guess the outer 'for' loop is being traversed only
once. I would be greatful if you could help me out. Thanx!
>>>f=1
for i in range(10,100):
... for j in range(2,i):
... if i%j==0:
... f=0
... break
... else: continue
... if f==1:
... print i,
...
Note that once you set f=0, it will never change.
Move the f=1 inside the outer loop. Also, the else clause is useless here;
best to use a bool for conditions; and it would benefit from better
variable names. Keeping the same structure:

for number in range(10,100):
is_prime = True
for divisor in range(2,number):
if number % divisor == 0:
is_prime = False
break
if is_prime:
print number,

Next step: for loops have an optional "else" clause, that gets executed
whenever the loop exits normally (in this case, when divisor goes up to
number, and the break statement is never executed). So you don't need
is_prime:

for number in range(10,100):
for divisor in range(2,number):
if number % divisor == 0:
break
else:
print number,

--
Gabriel Genellina

Jun 12 '07 #3

P: n/a
On Tue, Jun 12, 2007 at 04:25:31AM -0000, why? wrote:
I've been having problem with the following code. It's supposed to
print the prime numbers between 10 and 100. But i'm not getting any
output, i.e. i guess the outer 'for' loop is being traversed only
once. I would be greatful if you could help me out. Thanx!
>f=1
for i in range(10,100):
... for j in range(2,i):
... if i%j==0:
... f=0
... break
... else: continue
... if f==1:
... print i,
...
Move "f=1" inside the outer loop:

for i in range(10,100):
f=1
for j in range(2,i):
if i%j==0:
f=0
break
else: continue
if f==1:
print i,

It gets set to 0 in the first iteration and never has another chance to
be set to 1 after that.

Jun 12 '07 #4

P: n/a
At 09:52 PM 6/11/2007, Dan Hipschman wrote:
>On Tue, Jun 12, 2007 at 04:25:31AM -0000, why? wrote:
I've been having problem with the following code. It's supposed to
print the prime numbers between 10 and 100. But i'm not getting any
output, i.e. i guess the outer 'for' loop is being traversed only
once. I would be greatful if you could help me out. Thanx!
>>f=1
>>for i in range(10,100):
... for j in range(2,i):
... if i%j==0:
... f=0
... break
... else: continue
... if f==1:
... print i,
...

Move "f=1" inside the outer loop:

for i in range(10,100):
f=1
for j in range(2,i):
if i%j==0:
f=0
break
else: continue
if f==1:
print i,

It gets set to 0 in the first iteration and never has another chance to
be set to 1 after that.
And of course the inner loop does too much work. Try:

for i in range(10,100):
f=1
max = int(i**.5 + 1)
for j in range(2,max):
if i%j==0:
f=0
break
else: continue
if f==1:
print i,

Dick Moores

Jun 12 '07 #5

P: n/a
On Jun 12, 6:57 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
[...]

for number in range(10,100):
is_prime = True
for divisor in range(2,number):
if number % divisor == 0:
is_prime = False
break
if is_prime:
print number,

Next step: for loops have an optional "else" clause, that gets executed
whenever the loop exits normally (in this case, when divisor goes up to
number, and the break statement is never executed). So you don't need
is_prime:

for number in range(10,100):
for divisor in range(2,number):
if number % divisor == 0:
break
else:
print number,
Oh my. Would it not be an idea to rename this "else" into a "finally"?
As Gabriel points out, the else-block gets executed after the for loop
exits *normally*. In that case, is the "else" not semantically
misleading? I would surely misunderstand it if I saw it the first time.

Jun 12 '07 #6

P: n/a
exhuma.twn skrev:
>for number in range(10,100):
for divisor in range(2,number):
if number % divisor == 0:
break
else:
print number,

Oh my. Would it not be an idea to rename this "else" into a "finally"?
As Gabriel points out, the else-block gets executed after the for loop
exits *normally*. In that case, is the "else" not semantically
misleading? I would surely misunderstand it if I saw it the first time.
"finally" would be at least equally confusing IMO, indicating that the
code is always called (although this would of course make it a
ridiculous construct).

/Nis
Jun 12 '07 #7

P: n/a
On 6/12/07, Nis Jørgensen <ni*@superlativ.dkwrote:
exhuma.twn skrev:
for number in range(10,100):
for divisor in range(2,number):
if number % divisor == 0:
break
else:
print number,
Oh my. Would it not be an idea to rename this "else" into a "finally"?
As Gabriel points out, the else-block gets executed after the for loop
exits *normally*. In that case, is the "else" not semantically
misleading? I would surely misunderstand it if I saw it the first time.

"finally" would be at least equally confusing IMO, indicating that the
code is always called (although this would of course make it a
ridiculous construct).

/Nis
--
http://mail.python.org/mailman/listinfo/python-list
I think finally would be semantically nicer than else. . . maybe
something like "andthen" instead (yeah that's ugly).

I mean, else works (so would herbivore), but the terminology is a bit weird..
Jun 12 '07 #8

P: n/a
En Tue, 12 Jun 2007 06:34:49 -0300, exhuma.twn <ex****@gmail.comescribió:
On Jun 12, 6:57 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>for number in range(10,100):
for divisor in range(2,number):
if number % divisor == 0:
break
else:
print number,

Oh my. Would it not be an idea to rename this "else" into a "finally"?
As Gabriel points out, the else-block gets executed after the for loop
exits *normally*. In that case, is the "else" not semantically
misleading? I would surely misunderstand it if I saw it the first time.
No - finally already has a meaning, "do this always, even if an exception
occurred before".
The "else" clause is fired when a condition is not met:

if condition:
do something when condition is true
else:
do something when condition is not true
while condition:
do something when condition is true
else:
do something when condition is not met
for x in iterable:
do something with x
else:
do something when there are no more x
You can think the above as:

while there are still values in iterable:
do something with the next value
else:
do something when there are no more items

--
Gabriel Genellina

Jun 12 '07 #9

P: n/a
On 6/12/07, Gabriel Genellina <ga*******@yahoo.com.arwrote:
En Tue, 12 Jun 2007 06:34:49 -0300, exhuma.twn <ex****@gmail.comescribió:
On Jun 12, 6:57 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
for number in range(10,100):
for divisor in range(2,number):
if number % divisor == 0:
break
else:
print number,
Oh my. Would it not be an idea to rename this "else" into a "finally"?
As Gabriel points out, the else-block gets executed after the for loop
exits *normally*. In that case, is the "else" not semantically
misleading? I would surely misunderstand it if I saw it the first time.

No - finally already has a meaning, "do this always, even if an exception
occurred before".
The "else" clause is fired when a condition is not met:

if condition:
do something when condition is true
else:
do something when condition is not true
while condition:
do something when condition is true
else:
do something when condition is not met
for x in iterable:
do something with x
else:
do something when there are no more x
You can think the above as:

while there are still values in iterable:
do something with the next value
else:
do something when there are no more items
This is a good way of phrasing it and I hope I can remember it,
because for..else always gives me trouble. To me, "else" indicates the
negative condition and I intuitively associate it with executing the
loop early, not normal exit. Personally, I think a different keyword
(maybe "after"?) would have done a better job of clarifying this.
Jun 12 '07 #10

P: n/a
En Tue, 12 Jun 2007 10:41:28 -0300, Chris Mellon <ar*****@gmail.com>
escribió:
On 6/12/07, Gabriel Genellina <ga*******@yahoo.com.arwrote:
>for x in iterable:
do something with x
else:
do something when there are no more x
You can think the above as:

while there are still values in iterable:
do something with the next value
else:
do something when there are no more items

This is a good way of phrasing it and I hope I can remember it,
because for..else always gives me trouble. To me, "else" indicates the
negative condition and I intuitively associate it with executing the
loop early, not normal exit. Personally, I think a different keyword
(maybe "after"?) would have done a better job of clarifying this.
Yes, maybe, but it's hard to find a keyword equally applicable to "for"
and "while" and creating two new keywords for essencially the same thing
would be too much... Anyway it's too late to be changed now.

--
Gabriel Genellina

Jun 12 '07 #11

P: n/a
On Jun 12, 7:55 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Tue, 12 Jun 2007 10:41:28 -0300, Chris Mellon <arka...@gmail.com>
escribió:
On 6/12/07, Gabriel Genellina <gagsl-...@yahoo.com.arwrote:
for x in iterable:
do something with x
else:
do something when there are no more x
You can think the above as:
while there are still values in iterable:
do something with the next value
else:
do something when there are no more items
This is a good way of phrasing it and I hope I can remember it,
because for..else always gives me trouble. To me, "else" indicates the
negative condition and I intuitively associate it with executing the
loop early, not normal exit. Personally, I think a different keyword
(maybe "after"?) would have done a better job of clarifying this.

Yes, maybe, but it's hard to find a keyword equally applicable to "for"
and "while" and creating two new keywords for essencially the same thing
would be too much... Anyway it's too late to be changed now.

--
Gabriel Genellina
Hmmm,
Would replacing the word 'else' with 'then' read better? The implied
meaning is if the loop terminates normally *then* also do this block.

- Paddy.

Jun 12 '07 #12

P: n/a
On 6/12/07, Gabriel Genellina <gagsl-...@yahoo.com.arwrote:
>for x in iterable:
> do something with x
>else:
> do something when there are no more x
>You can think the above as:
>while there are still values in iterable:
> do something with the next value
>else:
> do something when there are no more items
@Gabriel--Hey thanx a lot! I had seen a similar 'for-else' clause
somewhere else as well and i was left wondering that how an 'else' got
coupled with 'for'. Anyways now am ok! This post clears a lot of
doubts.

Jun 13 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.