473,513 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with following python code

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
12 1224
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3047
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
49
2556
by: Mark Hahn | last post by:
As we are addressing the "warts" in Python to be fixed in Prothon, we have come upon the mutable default parameter problem. For those unfamiliar with the problem, it can be seen in this Prothon...
11
2509
by: Faheem Mitha | last post by:
Hi, I'm not sure what would be more appropriate, so I'm ccing it to both alt.comp.lang.learn.c-c++ and comp.lang.python, with followup to alt.comp.lang.learn.c-c++. While working with a...
1
2078
by: | last post by:
Hello, I tried to build Python 2.4.1 on a Reliant Unix system. Just after the python executable program has been built, I get the following error: ==== begin make output === CC -W1...
2
2012
by: tony.ha | last post by:
Hello, I have downloaded the PyPE2.1-win-unicode.zip, after unzip it with winzip into PyPE2.1-win-unicode dierctory on window XP. I try to run "pype.exe" by double click on it, but nothing has...
1
2757
by: neha | last post by:
hi, i m trying to integrate python with apache on linux.For this i m using mod_python. I dont see any problem with the versions of python,apache and mod_python i m using. the versions i m using...
9
1403
by: Marshall Dudley | last post by:
I am trying to install python, but am having problems. I did what the README file said, and it created an executible code in the current directory as it said it would when I typed "make". It...
1
1980
by: boney | last post by:
hello All, I am totally unknown to python language.. i need to install mod_python to embed python interpreter with the Apache server, in order to use Trac with Apache Web Server i am using...
4
2513
by: vedrandekovic | last post by:
Hi, I have already install Microsoft visual studio .NET 2003 and MinGw, when I try to build a extension: python my_extension_setup.py build ( or install ) , I get an error: LINK : fatal...
1
2557
by: Benke | last post by:
Hello, I'm quite new to Python and embedding python in c++. I'm trying to write a function that i can use to call a python function. It should take 3 arguments, the name of the python file, the...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7161
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.