Python 3:如何在使用输入时忽略换行符()

问题描述:

while count != 5: 

    input_text = input("Please insert a number of lines of text \n") 

    if count != 5: 
    print("Count is " + str(count)) 

对于上面的代码,当系统提示提供输入时,是否粘贴具有多个换行符的文本。代码将运行换行符的数量!我只想让它为整个文本运行一次。Python 3:如何在使用输入时忽略换行符()

谁能帮助?

您可以使用sys.stdin.read()但它需要您手动发送EOT字符:

>>> import sys 
>>> x = sys.stdin.read() 
the quick brown fox 
jumped over the lazy 
dog 
>>> print(x) 
the quick brown fox 
jumped over the lazy 
dog 

>>> 

通知,在结束后,我贴我用输入然后CTRL-d

我没有找到确切的答案给你问题,但我注意到,当你在shell中复制多行文本时,它只会将第一行文本分配给input_text,然后再次运行并指定第二行到input_text,再次运行input_text的第三行。你看。

我认为输入语句不适用于多行,虽然你可以找到一些解决方法。

这这里代码显示了循环每次跑了如何变量变化太大的,下次你行复制到外壳:

count = 0 
while True: 
    count += 1 
    input_text = input("Please insert a number of lines of text \n") 
    print("Count is " + str(count)) 
    print("what the variable is set to first time its running the loop: ",input_text,"\n")