如何忽略输入中的返回/输入

时间:2017-12-01 19:02:57

标签: python

当我将一些文本粘贴到其中有一个返回的python(例如一段段落文本)时,输入就会停止,因为python认为我已经按下了回车键。

我想要它只是在输入所有内容后才接受输入,而不仅仅是第一次返回。

这是一个例子:

代码:

text = input('Enter: ')
print(text)

输入:

In another moment down went Alice after it, never once considering how
in the world she was to get out again.

The rabbit-hole went straight on like a tunnel for some way, and then
dipped suddenly down, so suddenly that Alice had not a moment to think
about stopping herself before she found herself falling down a very deep
well.

输出:

In another moment down went Alice after it, never once considering how

4 个答案:

答案 0 :(得分:0)

虽然收到的行不是空的,但请抓住另一行。

input_received = True
input_string = ""
while input_received:
  new_input = input()
  if new_input:
    input_string += new_input
  else:
    input_received = False

答案 1 :(得分:0)

当用户实际输入文本时,您需要继续使用input()获取下一行的读数。当他们输入文本时,您需要记住将此文本连接到text字符串以及\n ,因为这可以确保该段落保持其形式。

使用while循环可以轻松实现上述步骤,该循环使用空字符串求值为 False 的事实:

text = ""
inpt = input()
while inpt:
    inpt = input()
    text += inpt + "\n"

所以如果我输入的内容如下:

Hello, this is line 1 and 
we are onto the next line 
and now onto the third is
this not a cool text para

然后运行print(text),我会在不同的行上获得正确的输出:

Hello, this is line 1 and 
we are onto the next line 
and now onto the third is
this not a cool text para

答案 2 :(得分:0)

import sys

print ('Enter: ', end='')
text = sys.stdin.readlines()[0].strip()
print(text)

答案 3 :(得分:-3)

对不起,我无法帮助您使用Python,但在C语言中,这可以通过以下方式实现:

cin.ignore(10000, '\n')