I'm new to Python, I've only done programming in Java before recently. I am writing a basic program in Python to print out the 1000th prime number and while I did get it to work, for some reason it's also printing out the next 7 numbers unless I use an unnecessary break:
import math
n=2
location =0
while location < 999 :
if location == 998 :
print n
n=n+1
srn = math.sqrt(n)
srn = int(srn)
while srn > 1 :
if n % srn == 0 :
break
elif srn==2 and n%srn != 0 :
location = location+1
srn = srn-1
prints
7919
7920
7921
7922
7923
7924
7925
7926
but
while location < 999 :
if location == 998 :
print n
break
n=n+1
srn = math.sqrt(n)
srn = int(srn)
while srn > 1 :
if n % srn == 0 :
break
elif srn==2 and n%srn != 0 :
location = location+1
srn = srn-1
prints
7919
Can anyone tell me why this is happening? Also when I was trying to fix this, I found that the shell would only print this once, then if I copied the code, whether I altered it or not, it wouldn't print anything. I would need to restart the shell each time I wanted to alter the code.