我知道我应该避免陷入这些情况,但我喜欢使用iPython并在实验期间保留变量。我将Spyder中的部分代码粘贴到中,让它们运行,然后检查变量等。现在,我有一部分代码如下:
for a in range(bignum):
<something>
except:
print('Badness')
我一开始就意识到我犯了一个错误,但是现在我不能用ctrl-C来阻止它,因为try/except只是打印消息并继续前进。有没有一种方法可以在不中断会话的情况下停止循环?
break
except Exception as e: # Any other exception
print(str(e)) # Displays the exception without raising it
break
for a in range(big_num):
pass # Do your thing
except KeyboardInterrupt:
print("You pressed ctrl c...")
except Exception as e:
print(str(e))