python - 如何在 Python 中使用 sys.exit()

标签 python python-3.x exit

player_input = '' # This has to be initialized for the loop

while player_input != 0:

    player_input = str(input('Roll or quit (r or q)'))

    if player_input == q: # This will break the loop if the player decides to quit

        print("Now let's see if I can beat your score of", player)
        break

    if player_input != r:

        print('invalid choice, try again')

    if player_input ==r:

        roll= randint (1,8)

        player +=roll #(+= sign helps to keep track of score)

        print('You rolled is ' + str(roll))

        if roll ==1:

            print('You Lose :)')

            sys.exit

            break

如果 roll == 1,我试图告诉程序退出,但什么也没有发生,当我尝试使用 sys.exit()< 时,它只是给我一条错误消息


这是我运行程序时显示的消息:

Traceback (most recent call last):
 line 33, in <module>
    sys.exit()
SystemExit

最佳答案

我觉得你可以用

sys.exit(0)

可以查一下here在 python 2.7 文档中:

The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like.

关于python - 如何在 Python 中使用 sys.exit(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14639077/

相关文章:

python - 使用 "df.sample(frac=1)"后如何删除添加的列?

php - 使用 Python/OpenCV 检测空心圆

python-3.x - 是否有 python 3 命令来清除输出控制台?

python - 如何为这些 if 语句创建循环?

javascript - 您可以使用 javascript 停止 WebAssembly 中当前正在运行的 c++ 吗?

python - 如何在 Python 中加速矩阵乘法?

python - 在python套接字编程中建立连接后如何获取客户端的IP地址?

python - 从 Python 执行 C 函数时防止 exit(1) 退出 Python

java - 捕获库的退出指令

python - 在 Python 中创建向前兼容的 OpenGL 3.x 上下文