簡體   English   中英

Python 鍵盤中斷不停止循環

[英]Python KeyboardInterrupt not stopping loop

macOS 終端中的 Python 3.10

我在 macOS 終端中啟動了這個簡單的腳本,它無限運行(所以在運行時請注意):

import itertools
for i in itertools.count(start=1):
    try:
        p = pow(2,i)
        print(p)
    except KeyboardInterrupt:
        print("Loop terminated")
        raise

然而,點擊鍵盤並不會終止循環。

我錯過了什么?

感謝大家的澄清。 問題是我假設KeyboardInterrupt會響應按下的任何鍵。 它實際上只響應 Ctrl-C。 使用 Ctrl-C 時一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM