[英]How can I not stop the program when ctrl+C is pressed?
I am trying to not stop the program when the user press Ctrl + C .当用户按下Ctrl + C时,我试图不停止程序。
Now it's like this:现在是这样的:
(programm running)
...
(user press ctrl+c)
You pressed ctrl+c
breaks
What I want is:我想要的是:
(programm running)
...
(user press ctrl+c)
You pressed ctrl+c
not breaks
How can I do this?我怎样才能做到这一点? Is it impossible?
这是不可能的吗?
I tried this code:我试过这段代码:
try:
while True:
userinput = input()
if userinput == "stop":
break
except (KeyboardInterrupt, SystemExit):
print('You pressed ctrl+c')
I tried adding pass
to except
, but It doesn't give the expected output.我尝试将
pass
添加到except
,但它没有给出预期的 output。
Move it inside the loop as I'm guessing you want to continue looping instead of exiting:将它移动到循环内,因为我猜你想继续循环而不是退出:
while True:
try:
userinput = input()
if userinput == "stop":
break
except (KeyboardInterrupt, SystemExit):
print('You pressed ctrl+c')
# or just pass to print nothing
you can easily implement the next step within the try & except or set a pass
after it.您可以轻松地在 try & except 中实现下一步,或者在它之后设置一个
pass
。
while True:
try:
userinput = input()
if userinput == "stop":
break
except (KeyboardInterrupt, SystemExit):
# or edit it to the next state in the program.
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.