简体   繁体   中英

I'm trying how to print this output to a text file on python

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM