如何在 Python 中使用 sys.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

原文由 user2027690 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 459
1 个回答

我想你可以使用

sys.exit(0)

您可以 python 2.7 文档中查看它:

可选参数 arg 可以是给出退出状态的整数(默认为零),或其他类型的对象。如果它是一个整数,则零被认为是“成功终止”,任何非零值被 shell 等认为是“异常终止”。

原文由 godidier 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题