我們在 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

更新時間:2020 年 2 月 26 日

347 次瀏覽

開啟你的 事業

透過完成課程獲得認證

立即開始
廣告
© . All rights reserved.