在使用 EOF 异常打破初始 input() 之后,如何启动一个新的 input() 实例?
how can I start a new input() instance after breaking the initial input() with EOF exception?
我需要从用户那里获取多行输入 - 使用 while 循环,但在启动另一个 input() 时遇到问题,因为 EOF "passed" 转到新的 input()
我尝试结合使用 sys stdin 和 func(),但不确定为什么会这样。
while True:
try:
list = input()
except EOFError:
break
input('input2:')
1) 不要使用 list
作为变量名。那是python.
中的保留字
2)您可以捕获一个 KeyboardInterrupt
,它将在 Ctrl + C
和 EOFError
捕获一个 Ctrl + D
:
之后停止循环
while True:
try:
list = input()
except (EOFError, KeyboardInterrupt):
break
input('input2:')
或者,您可以启动循环并让循环在设定条件下退出:
my_input = input()
while my_input: # Break if nothing was inputted
print(f"Inputed: {my_input}")
my_input = input()
input('input2:')
我需要从用户那里获取多行输入 - 使用 while 循环,但在启动另一个 input() 时遇到问题,因为 EOF "passed" 转到新的 input()
我尝试结合使用 sys stdin 和 func(),但不确定为什么会这样。
while True:
try:
list = input()
except EOFError:
break
input('input2:')
1) 不要使用 list
作为变量名。那是python.
中的保留字
2)您可以捕获一个 KeyboardInterrupt
,它将在 Ctrl + C
和 EOFError
捕获一个 Ctrl + D
:
while True:
try:
list = input()
except (EOFError, KeyboardInterrupt):
break
input('input2:')
或者,您可以启动循环并让循环在设定条件下退出:
my_input = input()
while my_input: # Break if nothing was inputted
print(f"Inputed: {my_input}")
my_input = input()
input('input2:')