1

I'm struggling to print the output to a text file. Right now whenever I run it, it just says none in the text file. Someone, please help.

def SieveOfEratosthenes():
    n = int(input("Enter a Number: "))
    file = open("primes.txt", "w")
    prime = [True for i in range(n+1)]
    pr1 = 2
    while (pr1 * pr1 <= n):
    
        if (prime[pr1] == True):

            for i in range(pr1 * pr1, n+1, pr1):
                prime[i] = False
                pr1 += 1

        for pr1 in range(2, n+1):
            if prime[pr1]:
                output = print(pr1)
        
    file.write(str(output))
    file.close()
    
if __name__ == '__main__':
    SieveOfErastothenes()
Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39
John
  • 11
  • 1

1 Answers1

1

So now that I'm a bit more clear on what you're trying to do, you want a text file that lists all the primes in the range 0 to the number input takes.

Three issues:

  1. Typo in function call
  2. Assigning the output of print(s) rather than s, or str(s)
  3. Output is a string, and you want to add the number to the end of the string, but you're instead changing output to just be the last number.

Check the spelling of your function call at the end, you wrote "SieveOfErastothenes" not "SieveOfEratosthenes"

You're also assigning output to a print() function, rather than just the string you probably want to put in your file. I don't believe print() would return a string, so just

So the function:

n = int(input("Enter a Number: "))
file = open("primes.txt", "w")
prime = [True for i in range(n+1)]
pr1 = 2
while (pr1 * pr1 <= n):  
    if (prime[pr1] == True):
        for i in range(pr1 * pr1, n+1, pr1):
            prime[i] = False
    pr1 += 1
output = ''
for pr1 in range(2, n+1):
    if prime[pr1]:
        output = (pr1)
  
file.write(str(output))
file.close()

With the input of 10 this created a file named primes.txt that had a 7 in it. Basically appears to just write the last prime from 0 in range to the input given in the command line to the file. If you're trying to get all primes, as I'm not sure exactly what your goal is, you'll have add a couple things but I think you're close.

Because the now working version is output = (pr1) whatever was in output will now just be what is in pr1 on the very last time that if statement fires in the loop. You can use output += pr1, which is the equivalent of output = output + pr1

So yet another edit:

output = 'Numbers: '
for i in range(0,10):
    output = str(i)

Output will be '9', because the range stops at 9, and you totally reassigned the variable output every time. That's why the 'Numbers: ' is gone.

But what you may want is:

output = 'Numbers: '
for i in range(0,10):
    output += str(i)

Will get you 'Numbers: 0123456789'

Just for extra clarity

output += str(i)

Is EXACTLY the same as:

output = output + str(i)

This is very common shorthand that you'll pick up quick if you haven't already.

Hopefully that clears up the last part.

And finally one last addition:

    output = ''
    for pr1 in range(2, n+1):
        if prime[pr1]:
            output += str(pr1) + ', '

Will probably get you close to what I think you're going for. I ran the script on my computer and it seems to work with that change. Hopefully I explained why you can't just use output = thing_i_want_to_add_to_the_end_of_output

Tyk
  • 68
  • 9
  • Also "output = print(pr1)" should probably be pr1, though I'm not sure if those all the issues yet either. – Tyk Nov 15 '21 at 19:28
  • I typed in "SieveOfErastothenes" when writing the question so it's all good in the code. I tried what you've shown me and it works to a degree but I need all of the prime numbers. So say when I enter 1000000. I need all prime numbers to print to a text file not just 999983. – John Nov 15 '21 at 19:38
  • Check my last edit- you're constantly changing output to be whatever is the last prime you found- you need to instead add the new primes to the string. If some extra formatting when it gets printed out, you can do something like output += str(pr1) + ',' which would create a growing list. There are other ways to do the same thing, like appending to the end of an array and such but this is probably the least beginner-confusing. – Tyk Nov 15 '21 at 19:41
  • Got it figured out, thanks for the help I appreciate it. – John Nov 15 '21 at 22:35
  • Can you mark my answer as correct if it works for you? – Tyk Nov 15 '21 at 23:23