# Trouble using for loop.

Page 1 of 1

## 3 Replies - 751 Views - Last Post: 01 October 2011 - 11:07 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=249393&amp;s=7ee254e28e88f3a1b7408c225f9fe3de&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cupcake18

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 01-October 11

# Trouble using for loop.

Posted 01 October 2011 - 09:35 AM

```def isprime(num):
num*=1.0
for i in range(2,int(num**0.5)+1):
if num/i==int(num/i):
return False
return True

for a in range(2,100):

if isprime(a):

for count in range(1,a):

print "PRIME NO.:",count,"=",a

break

```

Why is "count" not getting incremented?

Is This A Good Question/Topic? 0

## Replies To: Trouble using for loop.

### #2 sepp2k

• D.I.C Lover

Reputation: 2682
• Posts: 4,284
• Joined: 21-June 11

## Re: Trouble using for loop.

Posted 01 October 2011 - 10:31 AM

You have a for loop which counts from 1 to a, however you use break to break out of the loop after the first iteration. So each time you go into the loop, count is set to 1 and then you exit the loop before count will be anything else. So that's why count is not incremented.

If you remove the break, count will get incremented, but probably not in the way you want (it's hard to say for sure of course, because you did not specify what you want, but I can't see a reason why you'd want the behavior you get when removing break).

To get the behavior that I suspect you want (though again it's hard to say when you never actually say what you want), you should get rid of the inner for loop and just manually keep track of a counter, which you increment each time you find a prime number.

### #3 cupcake18

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 01-October 11

## Re: Trouble using for loop.

Posted 01 October 2011 - 10:45 AM

sepp2k, on 01 October 2011 - 10:31 AM, said:

(it's hard to say for sure of course, because you did not specify what you want, but I can't see a reason why you'd want the behavior you get when removing break).

Sorry! My bad. I want the output to print primes till a specific number. Like, if I want to know the 146th number or something I want it in the output. Not sure if I'm being clear or not. :S
Something like:
Prime No.: 6=13

### #4 cupcake18

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 01-October 11

## Re: Trouble using for loop.

Posted 01 October 2011 - 11:07 AM

sepp2k,
Removed the inner for loop like you said and made little changes to the code. It's working! Thanks!