3

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 2

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

sys.exit also "only" raises an exception, as noted in its docstring: "Exit the interpreter by raising SystemExit(status)."
Note that the exit() function is designed for use from the shell, not from applications. In any code you are writing, you want sys.exit().
I don't think this fully answers their question. Are they not asking how sys.exit() would be called when the user presses ctrl+d?
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.