Python-停止无限循环的键盘命令? [英] Python - A keyboard command to stop infinite loop?
问题描述
Possible Duplicate:
Why can't I handle a KeyboardInterrupt in python?
我在玩一些Python代码并创建了一个无限循环:
I was playing around with some Python code and created an infinite loop:
y = 0
x = -4
itersLeft = x
while(itersLeft<0):
y = y + x
itersLeft = itersLeft - 1
print "y = ",y, "itersLeft = ", itersLeft
print y
是否有键盘快捷键可以让我停止循环-允许我修复循环然后重新启动循环?
Is there a keyboard shortcut that would allow me to stop the looping - allowing me to fix the loop and then restart it?
我尝试了 Ctrl + C ,但没有任何运气.如果有帮助,我正在使用Windows 7环境.
I've tried Ctrl+C and didn't have any luck. If it helps I'm using a Windows 7 environment.
谢谢.
编辑
我还应该提到我正在使用Aptana Studio 3,并尝试在其中运行 Ctrl + C 命令.它在那里不起作用-但可以在常规控制台中尝试正常运行.我以为一定是因为Aptana环境.
I should have also mentioned that I'm using Aptana Studio 3 and attempted to run the Ctrl+C command within that. It doesn't work there - but trying it within the regular console works fine. I'm assuming it must be because of the Aptana environment.
推荐答案
Ctrl + C 是您所需要的.如果没有用,请加倍努力. :-)当然,您也可以关闭外壳程序窗口.
Ctrl+C is what you need. If it didn't work, hit it harder. :-) Of course, you can also just close the shell window.
您没有提到这种情况.作为最后的选择,您可以编写一个包含taskkill /im python.exe
的批处理文件,并将其放在桌面,开始"菜单等上,并在需要杀死一个失控的脚本时运行它.当然,它将杀死所有个Python进程,所以要小心.
You didn't mention the circumstances. As a last resort, you could write a batch file that contains taskkill /im python.exe
, and put it on your desktop, Start menu, etc. and run it when you need to kill a runaway script. Of course, it will kill all Python processes, so be careful.
这篇关于Python-停止无限循环的键盘命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!