我需要编写一个 python 代码来打印输入,如下所示:
while (True):
output = raw_input()
print output
Run Code Online (Sandbox Code Playgroud)
但是当我想结束循环时,我使用了 Ctrl_D,它说:
File "./digits.py", line 6, in <module>
output = raw_input()
EOFError
Run Code Online (Sandbox Code Playgroud)
我该如何解决?如果可能的话请给我一些简单的方法,因为这是我第一次用Python编写。
这EOFError是一个可以用try-捕获的异常except。break这里我们使用关键字 if anEOFError被抛出来打破循环:
while True:
try:
output = raw_input()
except EOFError:
break
print(output)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3765 次 |
| 最近记录: |