python - 如何在运行时结束python程序?

标签 python python-2.7

我自学python。我在这里遇到了一个问题......“人类玩家也可以随时通过按下 Control-D 序列来结束游戏。”我该怎么做。我应该使用什么样的功能? 谢谢。

最佳答案

你可以试试 sys.exit() :

import sys
...
sys.exit()

还有一个更标准的 exit() 函数(您不需要为此导入任何东西)。但是,如文档中所述,这与 sys.exit() 之间存在一个显着差异:

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.

关于python - 如何在运行时结束python程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14107436/

相关文章:

python - Python 中 ND 数组的“删除”命令

python - 重试python中脚本/程序停止的地方,记住变量值

python - 如何使用元组列表中的元组中的值作为函数的参数?

python - "gcc"和 "libxml2"出错时如何安装lxml

Python 多维数组作为单个列表

python - 在 tensorflow 中获取 ValueError 说我的形状不兼容

python - 我不能使用 matplotlib

python - 将子字符串替换为列表中的项目

python - SQLAlchemy/Flask/Postgres编程错误: <unprintable ProgrammingError object>

python - TypeError:不支持的操作数类型 |: 'int' 和 'str'