[英]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.所以现在我对您要做什么有了更清楚的了解,您需要一个文本文件,其中列出 0 到数字输入范围内的所有素数。
Three issues:三个问题:
Check the spelling of your function call at the end, you wrote "SieveOfErastothenes" not "SieveOfEratosthenes"最后检查函数调用的拼写,你写的是“SieveOfEratosthenes”而不是“SieveOfEratosthenes”
You're also assigning output to a print() function, rather than just the string you probably want to put in your file.您还将输出分配给 print() 函数,而不仅仅是您可能想要放入文件中的字符串。 I don't believe print() would return a string, so just我不相信 print() 会返回一个字符串,所以只是
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.输入 10 这会创建一个名为 primes.txt 的文件,其中包含 7。 Basically appears to just write the last prime from 0 in range to the input given in the command line to the file.基本上似乎只是将范围内从 0 到命令行中给出的输入的最后一个素数写入文件。 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.因为现在的工作版本是 output = (pr1) 输出中的任何内容现在都将是 pr1 中最后一次 if 语句在循环中触发的内容。 You can use output += pr1, which is the equivalent of output = output + pr1可以使用output += pr1,相当于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.输出将为“9”,因为范围在 9 处停止,并且您每次都完全重新分配了变量输出。 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'会给你“号码: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希望我解释了为什么你不能只使用 output = thing_i_want_to_add_to_the_end_of_output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.