How to use sys.exit() in Python

exit, python, python-3.x

Solution

I think you can use

sys.exit(0)

You may check it here in the python 2.7 doc:

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.

Problem

``` 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 ``` 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 ```

Original source