1

so I'm writing a little text adventure and have lots of nested if statements. However, there comes times where I want to terminate the program in one of these if statements to give a "GAME OVER". I've tried quit() and exit(), but at best they still output an error message:

Traceback (most recent call last):
  File "<string>", line 9, in 0
SystemExit: 0

I'm not using any try/except statements, just if statements. For example:

import sys
if True:
    print "Hello guv'na"
    if True:
       print "Good day, Good day"
       sys.exit(0)
if True:
    print "What's all this then?"

yields:

Hello guv'na
Good day, Good day
Traceback (most recent call last):
  File "/Users/austin/test.py", line 6, in 0
builtins.SystemExit: 0

I just want the program to terminate after "Good day, good day" without a message. is this possible?

Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69
Austin
  • 21
  • 3
  • try `import os` and use `os._exit(code)` – Brian Tiffin Dec 25 '15 at 17:46
  • 3
    Welcome to StackOverflow. Please provide a [mcve]. – Cong Ma Dec 25 '15 at 17:47
  • 1
    The normal way to force an exit from Python is to `import sys` at the top of your file, then execute `sys.exit(0)` which will exit silently with `0` status, i.e. no error. – Tom Karzes Dec 25 '15 at 17:48
  • 2
    [How to exit from Python without traceback?](http://stackoverflow.com/questions/1187970/how-to-exit-from-python-without-traceback), [Terminating a Python script](http://stackoverflow.com/questions/73663/terminating-a-python-script) – GingerPlusPlus Dec 25 '15 at 17:58
  • How are you executing your script? Maybe you were not the author of the try/except block, but you are using a framework that does so. Also, the fact that your traceback contains `` is a strong signal that your code is being executed via `exec` (in other words, the code you have shown us is not the whole code that is being executed) – Andrea Corbellini Dec 25 '15 at 18:05
  • Side note. If you ever split your code in functions for, say, different sections on the adventure, then you can use return. – Reti43 Dec 25 '15 at 18:06
  • it seems that you launch the script from within a certain editor/idea and that tools adds some wrapper code around the code you actually type. try to run from command line directly and see if it is still reproducible. – mnagel Dec 25 '15 at 18:12

2 Answers2

0

If you are seeing that exception, it means that something is catching and printing it.

My guess is that you have a try/except block like this:

try:
    do_something()
except:
    print_exc()

Here the except: clause does not have an exception type specified, which means that all kind of exceptions (including SystemExit) will be caught.

Bare except: clauses are meant for cleanup code and generally re-raise the exception after catching it. Avoid them unless you absolutely need them.

Use this instead:

try:
    do_something()
except Exception:
    print_exc()

This way, you'll catch only Exception subclass, i.e. all standard exceptions except SystemExit, KeyboardInterrupt and GeneratorExit.

Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69
0

The brute force approach (below) should do what you want (well kill the program anyway).

import os
os.system('pkill python')
J damne
  • 72
  • 7