Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 466
user2027690 Avatar asked Feb 01 '13 03:02

user2027690


People also ask

What is the use of sys exit () in Python?

exit() function allows the developer to exit from Python. The exit function takes an optional argument, typically an integer, that gives an exit status. Zero is considered a “successful termination”.

How do you exit Python code?

Ctrl + C on Windows can be used to terminate Python scripts and Ctrl + Z on Unix will suspend (freeze) the execution of Python scripts. If you press CTRL + C while a script is running in the console, the script ends and raises an exception.

How do I exit 1 in Python?

The standard convention for all C programs, including Python, is for exit(0) to indicate success, and exit(1) or any other non-zero value (in the range 1.. 255) to indicate failure. Any value outside the range 0.. 255 is treated modulo 256 (the exit status is stored in an 8-bit value).

What is the difference between exit () and SYS exit ()?

exit is a helper for the interactive shell - sys. exit is intended for use in programs. The site module (which is imported automatically during startup, except if the -S command-line option is given) adds several constants to the built-in namespace (e.g. exit ).


3 Answers

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.

like image 71
godidier Avatar answered Oct 06 '22 12:10

godidier


sys.exit() raises a SystemExit exception which you are probably assuming as some error. If you want your program not to raise SystemExit but return gracefully, you can wrap your functionality in a function and return from places you are planning to use sys.exit

like image 31
Abhijit Avatar answered Oct 06 '22 11:10

Abhijit


you didn't import sys in your code, nor did you close the () when calling the function... try:

import sys
sys.exit()
like image 8
Pedro Fontes Avatar answered Oct 06 '22 12:10

Pedro Fontes