You set one variable called prime ten thousand times in 1 , then 9998 times, possibly setting it to 0 and finally (if it is not set to 0), it displays one incomplete line (without a line, end). I suspect that is not what you want! Maybe something like ...:
output = open("output.dat", 'w') for i in range(2, 10000): prime = 1 for j in range(2, i-1): if i%j == 0: prime = 0 break if prime == 1: output.write(str(i) + " " ) output.close() print "writing finished"
Pay attention to the different indentation very much from what you posted. I also used break to break out of the inner loop, which I think was what you had in mind when you wrote j = i - 1 (which would actually have no effect, since j would just have set it the next natural value in the very next segment of this inner cycle, which will still work to the end).
source share