我怎样才能做一个输入字段的多行? PYTHON

how can i do multiple lines of an input field? PYTHON

python

input(write some text: )

我想要它输出的是:

write some text: it was a good day
today as i went to a park

然后按两次回车键继续其他代码行

print("enter 'quit' at end of your text")
print("type your text here")
# declare variable to strore string
text = ""
stop_word = "quit"
while True:
    line = input()
    if line.strip() == stop_word:
        break
    # \n is new line, %s is string formatting
    # str.strip() will remove any whitespace that is at start or end
    text += "%s\n" % line.strip()
print(text)

如果你想使用 3 个空行 作为停用词:

print("Please enter 3 blank lines to terminate")
print("type your text here")
# declare variable to strore string
text = ""
stop_word = "quit"
counter = 0
while True:
    line = input()
    if line.strip() == '':
        counter += 1
        if counter == 3:
            break
    # \n is new line, %s is string formatting
    # str.strip() will remove any whitespace that is at start or end
    text += "%s\n" % line.strip()
print(text)

对于结果,如果要去掉空行:

print("Please enter 3 blank lines to terminate")
print("type your text here")
# declare variable to strore string
text = ""
stop_word = "quit"
counter = 0
while True:
    line = input()
    if line.strip() == '':
        counter += 1
        if counter == 3:
            # break terminates while loop
            break
        # continue go to start of while loop
        continue
    # \n is new line, %s is string formatting
    # str.strip() will remove any whitespace that is at start or end
    text += "%s\n" % line.strip()
print(text)

您可以使用 sys.stdin.readlines 读取多行,并使用 ctrl + z (在 windows 上)/ ctrl + d.

换行
import sys

msg = sys.stdin.readlines()