34
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
Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
user2027690
  • 419
  • 3
  • 6
  • 6
  • Please try to post a complete code snippet - where is `player` coming from for instance? I've also re-tagged for Python 3.x – Jon Clements Feb 01 '13 at 03:14
  • 6
    The problem is that you are running your code into IDLE. IDLE catches all exceptions(even SystemExit) and thus you see that traceback. To see how thing would normally go simply run your python program into a python shell (launch python from the terminal/command prompt) – Bakuriu Feb 01 '13 at 06:49

6 Answers6

36

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.

godidier
  • 923
  • 16
  • 34
10

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

import sys
sys.exit()
Pedro Fontes
  • 164
  • 2
  • 12
8

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

Abhijit
  • 62,056
  • 18
  • 131
  • 204
4

In tandem with what Pedro Fontez said a few replies up, you seemed to never call the sys module initially, nor did you manage to stick the required () at the end of sys.exit:

so:

import sys

and when finished:

sys.exit()
mana
  • 109
  • 1
  • 6
2

Using 2.7:

from functools import partial
from random import randint

for roll in iter(partial(randint, 1, 8), 1):
    print 'you rolled: {}'.format(roll)
print 'oops you rolled a 1!'

you rolled: 7
you rolled: 7
you rolled: 8
you rolled: 6
you rolled: 8
you rolled: 5
oops you rolled a 1!

Then change the "oops" print to a raise SystemExit

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
1

Real-World Example

Option 1: sys.exit()

Exits Python and raising the SystemExit exception.

import sys
try:
  sys.exit("This is an exit!")
except SystemExit as message:
  print(message)

Output:

This is an exit!

Option 2: sys.exit()

Exits Python without a message

import sys
sys.exit()

# runtime: 0.07s 
Stas Sorokin
  • 3,029
  • 26
  • 18