473,395 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1215
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.