0

As a simple exercise I'm trying to print all primes up to 500 into a text file but I'm unsure how to correctly insert the write code into the for loop, all that is currently output is the last prime (499 in this case).

for num in range(2,500): prime = True for i in range(2,num): if (num%i==0): prime = False if prime: print(num) with open("prime.txt", "a") as prime: prime.write(str(num)+ '\n') 

Any advice greatly appreciated.

2
  • 2
    your write statement is out of the loop, so why should you expect it to run on each iteration? Commented Nov 27, 2017 at 14:17
  • I know the write statement is in the incorrect place, I simply included it to see whether or not I was using the correct formatting for the statement. Commented Nov 27, 2017 at 14:24

6 Answers 6

1

Writting to file out of loop, you need move it to loop body:

with open("prime.txt", "a") as file_prime: for num in range(2,500): prime = True for i in range(2,num): if (num%i==0): prime = False if prime: print(num) file_prime.write(str(num)+ '\n') 
Sign up to request clarification or add additional context in comments.

Comments

1

You need to write to the file inside the for loop:

with open('prime.txt', 'a') as prime_file: for num in range(2, 500): prime = True for i in range(2, num): if (num % i == 0): prime = False break # <- Also added this. You should stop iterating once you know the number is not prime if prime: print(num) prime_file.write('{}\n'.format(num)) # <- Write to file if the number is prime 

You may also want to take a look at generators:

def generate_primes(max): for num in range(2, max): prime = True for i in range(2, num): if num % i == 0: prime = False break if prime: yield num with open('prime.txt', 'a') as prime_file: lines = map('{}\n'.format, (p for p in generate_primes(500))) prime_file.writelines(lines) 

Comments

0

You can either print each prime as you find it by putting your loop under the with open... context and writing each iteration:

with open("prime.txt", "a") as f: for num in range(2,500): # find the next prime if prime: f.write(str(num)+ '\n') 

Or probably better since you are working with small primes and can fit them all in memory, just store them in an array and print the whole array at once:

primes = [] # empty array to start for num in range(2,500): # find the next prime if prime: primes.append(num) with open("prime.txt", "w") as f: f.write('\n'.join(primes)) 

The last line will write all of your primes, separated by new lines, at one time to your text file, rewriting it each time you run the script (rather than continually appending, which is what the first version does).

Comments

0
with open("prime.txt", "a") as fd: for num in range(2,500): prime = True for i in range(2,num): if (num%i==0): prime = False if prime: #print(num) fd.write(str(num)+ '\n') 

Comments

0

Put your with open("prime.txt", "a") as prime: prime.write(str(num)+ '\n') within the for loop. That may help because it currently only runs through that process once it has finished the for loop iteration.

Comments

0

You can do it:

for num in range(2,500): prime = True for i in range(2, num): if (num % i == 0): prime = False if prime: print(num) with open("prime.txt", "a") as prime: prime.write(str(num) + '\n') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.