に機能を殺すためにSIGINTを使用した:例として、次のコードを取るのPython 3
import signal
import time
def stop(signal, frame):
print("You pressed ctrl-c")
# stop counter()
def counter():
for i in range(20):
print(i+1)
time.sleep(0.2)
signal.signal(signal.SIGINT, stop)
while True:
if(input("Do you want to count? ")=="yes"):
counter()
は、どのように私はそれがプロンプトに戻りますので、counter()
機能を殺す、または破損するstop()
機能になるだろうか?
出力例:
Do you want to count? no
Do you want to count? yes
1
2
3
4
5
6
7
You pressed ctrl-c
Do you want to count?
私は、Python 3.5.2を使用しています。
私はそれが保存するスペースの量のためにこれが本当に好きです:) –