I study python by myself. And i got a problem here.."The human player may also end the game by pressing the Control-D sequence at any time." How can i do this.what kind of function should i use? Thanks.
2 Answers
You could try sys.exit():
import sys
...
sys.exit()
There is also a more standard exit() function (for which you don't need to import anything). However there is one notable difference between this and sys.exit() as noted in the documentation:
Since
exit()ultimately "only" raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.
3 Comments
sys.exit also "only" raises an exception, as noted in its docstring: "Exit the interpreter by raising SystemExit(status)."exit() function is designed for use from the shell, not from applications. In any code you are writing, you want sys.exit().sys.exit() would be called when the user presses ctrl+d?The basic logic here is to handle SIGQUIT keyboard interrupt. There are many kind of interrupts, Ctrl+C (SIGINT), Ctrl+D (SIGQUIT), etc. This SOF thread discusses catching Ctrl+C signal in python. Based on the same lines, its not hard to catch Ctrl+D. This python documentation provides more insights.