简体   繁体   English

我正在尝试如何将此输出打印到 python 上的文本文件

[英]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:三个问题:

  1. Typo in function call函数调用中的错字
  2. Assigning the output of print(s) rather than s, or str(s)分配 print(s) 而不是 s 或 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"最后检查函数调用的拼写,你写的是“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.

相关问题 如何将打印 output 写入 Python 中的文本文件? - How can I write the print output to a text file in Python? 我正在尝试将 bibles.print(“Gen”, 1, 1) output 保存到文本文件中。 我该怎么做呢? - I am trying to save the bibles.print(“Gen”, 1, 1) output to a text file. How do I do this? 我正在尝试将文本文件转换为字典或列表Python - I'm trying to convert a text file to dictionary or a list Python 如何在Python中将(if-else)打印语句的输出重定向到文本文件 - How to redirect the output of print statement of (if-else) to a text file in Python 如何从python中的列表打印文本文件中的输出? - How to print the output in text file from the list in python? 使用Python将输出打印到文本文件(.txt) - Print output to text file (.txt) using Python 我是 python 的新手,我想弄清楚如何在同一行上一次打印一个字母 - I'm new to python and I am trying to figure out how to print letters one at a time on the same line 如何让 python 在 HTML 文件中打印此程序的 output? - How can I get python to print the output of this program in an HTML file? 我试图将已格式化的 CSV 文件内容输出到文本通道中,但是当我这样做时,它是在无序的行中。 我该如何解决? - I'm trying to output the already formatted contents of a CSV file into a text channel, but when I do so it's in unordered rows. How can I fix it? 我正在尝试通过 python 打开文件 - I'm trying to open a file via python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM