如何用键盘命令停止Python中的无限循环?

如何用键盘命令停止Python中的无限循环?

任何循环都是为了执行一定次数或直到满足某个条件。但是,如果条件没有出现,循环就会无限重复。这种无限循环需要通过生成键盘中断来强制停止。按下ctrl-C可以停止无限循环的执行。

>>> while True:
print ('hello')


hello
hello
hello
hello
hello
hello
Traceback (most recent call last):
File "<pyshell#18>", line 2, in <module>
print ('hello')
KeyboardInterrupt

阅读更多:Python 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程