在以下程序中
import itertools
for i in itertools.count():
print (i)
我想使用键盘按下事件停止此事件。
答案 0 :(得分:1)
您可以等待KeyboardInterrupt
例外,然后离开:
import itertools
try:
# Stay inside the loop until Ctrl+C is pressed
for i in itertools.count():
print (i)
except KeyboardInterrupt:
pass
# Go on...