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.