问题
while count != 5:
input_text = input("Please insert a number of lines of text \n")
if count != 5:
print("Count is " + str(count))
For the code above, when prompted to supply input, if I paste in a text with multiple line breaks. The code will run for the number of line breaks! I just want it to run ONCE for the entire text.
Can anyone help?
回答1:
You can use sys.stdin.read()
but it will require you to manually send the EOT character:
>>> 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
>>>
Notice, at the end after I pasted I use Enter and then ctrl-D.
回答2:
I didn't find the exact answer for you question however i did notice that when you copy in multiple lines of text in the shell it only assigns the first line of text to input_text, then runs again and assigns the second line to input_text, runs agains and third line of input_text. You see.
I think the input statement is not ment for multiple lines though you can sure find some workaround.
This code here shows how each time the loop is ran the variable changes too the next line of that you copied into shell:
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")
来源:https://stackoverflow.com/questions/42871100/python-3-how-to-ignore-line-breaks-when-using-input