为什么使用 Wing IDE 101 时 KeyboardInterrupt 在 Python 中不起作用?

Why won't KeyboardInterrupt work in Python when using Wing IDE 101?

我正在尝试循环执行某些操作(在此示例中打印为 100)并随时按 'Ctrl+C' 取消计数。

我正在使用的测试代码如下,但这不起作用(编辑:它在从终端启动的脚本中起作用,但在 python 中的 运行 时不起作用 shell 我的 IDE - 联队 101)。 KeyboardInterrupt 不会捕获 'Ctrl+C' 命令或任何其他键。

import time     

i = 1
while (i < 101):
    try:
        print '%d percent complete' % i
        time.sleep(0.5)
        i += 1
    except KeyboardInterrupt:
        break 

我读过这个问题 here and I'm pretty sure that's not my problem because I've put the 'time.sleep(0.5)' command in to slow the program down. I've also read this question 但因为它已经 5 岁了,我认为这个错误现在应该已经修复了吗?我知道我可以使用线程来实现我想要的,但如果只是为了学习,我想知道为什么这种方法不起作用。

我在 Ubuntu 14.04 中使用 python 2.7.6,对于解决此问题的任何帮助或建议,我将不胜感激。 (编辑:我知道代码可以独立运行,但我仍然想知道为什么它在 Wing 101 IDE 的 python shell 中不起作用)

编辑 1

我试过按照建议将 while 循环放在 try 块中:

try:
    i = 1
    while (i < 101):        
        print '%d percent complete' % i
        time.sleep(0.5)
        i += 1
except KeyboardInterrupt:
    break 

但不幸的是,这也不起作用,我收到以下错误:

Traceback (most recent call last):
  File "/home/matt/autosys_repo1/python_test_scripts/test_refresh_line.py", line 13, in <module>
    break
'break' outside loop: <string>, line 13

这是 Wing IDE 101 的限制。停止这种循环的唯一方法是重新启动 shell。