Ric*_*ckD 55 python user-input input
我想创建一个Python程序,它接受多行用户输入.例如:
This is a multilined input.
It has multiple sentences.
Each sentence is on a newline.
Run Code Online (Sandbox Code Playgroud)
如何接收多行原始输入?
jam*_*lak 79
sentinel = '' # ends when this string is seen
for line in iter(raw_input, sentinel):
pass # do things here
Run Code Online (Sandbox Code Playgroud)
要将每一行作为字符串,您可以执行以下操作:
'\n'.join(iter(raw_input, sentinel))
Run Code Online (Sandbox Code Playgroud)
Python 3:
'\n'.join(iter(input, sentinel))
Run Code Online (Sandbox Code Playgroud)
继续读取行,直到用户输入空行(或更改stopword为其他行)
text = ""
stopword = ""
while True:
line = raw_input()
if line.strip() == stopword:
break
text += "%s\n" % line
print text
Run Code Online (Sandbox Code Playgroud)
小智 6
或者,您可以尝试 sys.stdin.read()
import sys
s = sys.stdin.read()
print(s)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43175 次 |
| 最近记录: |