如何在Python中使用sys.exit() [英] How to use sys.exit() in Python
问题描述
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()
I am trying to tell the program to exit if roll == 1
but nothing is happening and it just gives me an error message when I try to use sys.exit()
这是我运行程序时显示的消息:
This is the message that it shows when I run the program:
Traceback (most recent call last):
line 33, in <module>
sys.exit()
SystemExit
推荐答案
我认为您可以使用
sys.exit(0)
您可以在python 2.7文档的此处进行检查. :
You may check it here in the python 2.7 doc:
可选参数arg可以是给出退出状态的整数(默认为零),也可以是其他类型的对象.如果是整数,则Shell等将零视为成功终止",而将任何非零值视为异常终止".
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中使用sys.exit()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!