473,625 Members | 3,329 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 1232
On 6/12/07, why? <ji*******@yaho o.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*******@yaho o.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.a r>
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.c omescribió:
On Jun 12, 6:57 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
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*******@yaho o.com.arwrote:
En Tue, 12 Jun 2007 06:34:49 -0300, exhuma.twn <ex****@gmail.c omescribió:
On Jun 12, 6:57 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3060
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 the context of a main window. The problem does not occur on HPUX or Linux. The following simple example code illustrates the problem and the work around I've come up with; However, I'd like, very much, to get rid of the kludgy work around....
49
2589
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 code sample where newbies expect the two function calls below to both print : def f( list= ): print list.append!(1) f() # prints
11
2525
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 random number generator in the context of a mixed Python/C++ programming problem. I encountered a vexing type conversion problem.
1
2085
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 -Blargedynsym -o python \ Modules/python.o \ libpython2.4.a -lresolv -lsocket -lnsl -ldl -lm
2
2016
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 happen, then when I run it with "run as..." using the right click button. I have the following Message in the pype.exe.log Traceback (most recent call last):
1
2781
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 are apache version2. mod_python v3.1.14 python2.4 The problem is,when i m running my python script,after starting apache
9
1406
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 seemed to say to copy the executable over to the /usr/local directory, which I did. If I type ./python in the directory I did the make in I get: execonn# ./python Python 2.4.3 (#1, May 31 2006, 07:50:04) ] on freebsd4
1
1987
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 : Python 2.4.3,
4
2523
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 error LNK1141: failure during build of exports file error: command '"C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\link.exe"' failed with exit status 1141.What shoud I
1
2566
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 python function name and a std:vector with arguments for the python function. This is my code: int main() { string arg1 = "runme"; //name of python file string arg2 = "sub"; // name of python function Py_Initialize();
0
8694
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8356
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6118
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4089
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4193
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
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 we have to send another system
1
1803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1500
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.