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/