python中处理键盘中断后如何返回执行循环
我有一个如下代码:python中处理键盘中断后如何返回执行循环,python,loops,keyboardinterrupt,Python,Loops,Keyboardinterrupt,我有一个如下代码: some_list = [] try: while True: ... code that uses the list ... except KeyboardInterrupt: ... code that modifies the list ... 处理此异常后,如何在不嵌套循环的情况下返回执行此循环?您可以简单地重新构造代码,以使用try/catch 请参见以下示例: from time import sleep some_list =
some_list = []
try:
while True:
... code that uses the list ...
except KeyboardInterrupt:
... code that modifies the list ...
处理此异常后,如何在不嵌套循环的情况下返回执行此循环?您可以简单地重新构造代码,以使用
try/catch
请参见以下示例:
from time import sleep
some_list = []
while True:
try:
print(some_list)
sleep(1)
except KeyboardInterrupt:
some_list.append("1")
捕获异常后不能返回循环。而是在循环中使用
try catch
块,以便在捕获异常后继续迭代。