title: Python - printing first 1000 primes doesn't stop
body: <p>There has been a problem in my latest code, that I do not know how to solve, at all. Basically, I am printing out the first 1000 primes - but my code prints out prime numbers well beyond the first 1000 primes limit. I have specified in my code to stop at 1000. Here is my code:</p>
  
  <pre><code>import itertools
  
  counter = 1
  
  while (counter &lt;= 1000):
      for i in itertools.count(start=3, step=1):
          for a in range(2, i/2):
              if (i % a == 0):
                  break
          else:
              print i
              counter += 1
  </code></pre>
  
  <p>I really do not know how to fix this. Can someone help?</p>
  
  <p><strong>Solution</strong></p>
  
  <p>I fixed this code by adding this piece of code inside the else block:</p>
  
  <pre><code>if (counter == 1000):
      sys.exit()
  </code></pre>