不确定如何从多行循环获取用户输入 - Python

时间:2022-11-29 17:24:34

I have been trying to create a program that lets users name, write and save documents, here is what I have come up with so far:

我一直在尝试创建一个程序,让用户可以命名,编写和保存文档,这是我到目前为止所提出的:

doc_name = str(input("Document Name: "))
end = ""
for line in iter(input, end):
    document = "\n".join(iter(input, end))
    pass
try:
    savefile = open("/home/" +doc_name+ ".txt", "w")
    savefile.write(x)
    savefile.close()
    print("Document - " +doc_name+ "\nSuccessfully saved.\n\n")
except:
    print("An error occurred.\nUnable to save document.\n\n")

The 'for loop' I have used was from the following page: Raw input across multiple lines in Python but I am unsure how to use the input from this loop, so I am able to save it to a textfile. I need the input in this line of code in the place of x:

我使用的'for循环'来自以下页面:Python中多行的原始输入,但我不确定如何使用此循环的输入,因此我可以将其保存到文本文件中。我需要在x代码的这行代码中输入:

savefile.write(x)

I am using Python 3.2.3 for this program (if that helps?). I would like to know how the user input entered during the for loop can be stored in a varible and then used at some other point in the program.

我在这个程序中使用Python 3.2.3(如果有帮助的话?)。我想知道在for循环期间输入的用户输入如何存储在varible中,然后在程序中的其他位置使用。

Thanks.

1 个解决方案

#1


doc_name = input("Document Name: ") # don't need to cast to str
end = ""
result = [] # I recommend initializing a list for the lines
for line in iter(input, end): # you only need this single input call
    result.append(line) # add each line to the list
try:
    # using "with" in this manner is guaranteed to close the file at the end
    with open("/home/" +doc_name+ ".txt", "w") as savefile:
        for line in result: # go through the list of lines
            # write each one, ending with a newline character
            savefile.write(line + '\n')
except IOError:
    print("An error occurred.\nUnable to save document.\n\n")
else: # print this if save succeeded, but it's not something we want to "try"
    print("Document - " +doc_name+ "\nSuccessfully saved.\n\n")

You only need to use pass when Python expects statements (such as in an indented block) but you have no statements for it to execute - it's basically a placeholder. The common use is when you want to define your program's functions (e.g., def myfunction(a, b):) but you don't have the actual content for them yet.

你只需要在Python期望语句时使用pass(例如在缩进块中),但是你没有执行它的语句 - 它基本上是一个占位符。常用的是你想要定义你的程序的功能(例如,def myfunction(a,b):)但你还没有它们的实际内容。

#1


doc_name = input("Document Name: ") # don't need to cast to str
end = ""
result = [] # I recommend initializing a list for the lines
for line in iter(input, end): # you only need this single input call
    result.append(line) # add each line to the list
try:
    # using "with" in this manner is guaranteed to close the file at the end
    with open("/home/" +doc_name+ ".txt", "w") as savefile:
        for line in result: # go through the list of lines
            # write each one, ending with a newline character
            savefile.write(line + '\n')
except IOError:
    print("An error occurred.\nUnable to save document.\n\n")
else: # print this if save succeeded, but it's not something we want to "try"
    print("Document - " +doc_name+ "\nSuccessfully saved.\n\n")

You only need to use pass when Python expects statements (such as in an indented block) but you have no statements for it to execute - it's basically a placeholder. The common use is when you want to define your program's functions (e.g., def myfunction(a, b):) but you don't have the actual content for them yet.

你只需要在Python期望语句时使用pass(例如在缩进块中),但是你没有执行它的语句 - 它基本上是一个占位符。常用的是你想要定义你的程序的功能(例如,def myfunction(a,b):)但你还没有它们的实际内容。