首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Python中使用sys.exit()

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

Stack Overflow用户
提问于 2018-12-11 05:59:56
回答 2查看 0关注 0票数 0
代码语言:javascript
复制
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()

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

代码语言:javascript
复制
Traceback (most recent call last):
 line 33, in <module>
    sys.exit()
SystemExit
EN

Stack Overflow用户

发布于 2018-12-11 14:08:57

我想你可以用

代码语言:javascript
复制
sys.exit(0)

你可以检查它这里在Python 2.7 DOC:

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

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档