我正在尝试为for循环编写一个终止序列,我看到的所有示例都是for while循环,下面是我的代码
我尝试使用while和my How to kill a while loop with a keystroke?在while中的答案,这允许我转义每个for循环,但它无限地继续下去,并且按照下面的说明它自己也失败了
results = []
try:
for i in sites:
do stuff for each site
except KeyboardInterrupt:
print 'Keyboard Interrupt: script cancelled'
import sys
sys.exit() 已尝试
try:
while True:
for i in sites:发布于 2017-07-22 00:30:49
你能不能把你的例子简化到最低限度,其中有你想解决的问题?一般来说,您的逻辑看起来很好。下面是一个捕获键盘中断并继续执行的示例:
import time
try:
time.sleep(1000)
except KeyboardInterrupt:
print('Caught keyboard interrupt.')我认为您的代码中存在另一个问题。
发布于 2019-01-20 02:28:12
对我来说,使用Ctrl-C总是很难判断python会或不会中断哪些循环。这是一个对我有效的解决方案:
for i in sites:
try:
do stuff for each site
except KeyboardInterrupt:
print('\nKeyboard Interrupt: script cancelled')
break这很简单,但由于某些原因,很难找到堆栈溢出问题的直接答案。
https://stackoverflow.com/questions/45242349
复制相似问题