Problem with following python code

W

why?

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!.... for j in range(2,i):
.... if i%j==0:
.... f=0
.... break
.... else: continue
.... if f==1:
.... print i,
....
 
T

Tim Leslie

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!

You need to switch these two lines to reset the flag each time around
the outer loop.

Cheers,

Tim
 
D

Dan Hipschman

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!
... 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.
 
G

Gabriel Genellina

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!
... 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,
 
D

Dick Moores

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
 
E

exhuma.twn

[...]

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.
 
?

=?ISO-8859-1?Q?Nis_J=F8rgensen?=

exhuma.twn skrev:
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
 
K

kaens

exhuma.twn skrev:


"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

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..
 
G

Gabriel Genellina

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
 
C

Chris Mellon

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.
 
G

Gabriel Genellina

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.
 
P

Paddy

En Tue, 12 Jun 2007 10:41:28 -0300, Chris Mellon <[email protected]>
escribió:





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.

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.
 
W

why?

@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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top