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.

4

2 回答 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.

于 2013-01-01T02:20:15.097 回答
0

这里的基本逻辑是处理 SIGQUIT 键盘中断。中断有很多种,Ctrl+C (SIGINT)、Ctrl+D (SIGQUIT) 等。 这个 SOF 线程讨论了在 python 中捕获 Ctrl+C 信号。基于同样的思路,不难理解 Ctrl+D。这个python 文档提供了更多的见解。

于 2013-01-01T18:24:24.267 回答