0

I'm new to Python and I'm trying to use the exit function from sys, but somehow it's not working on my computer. This is my code:

from sys import exit

userResponse = input ("n or y?")
if not (userResponse =="n" or userResponse =="y") :
exit ("ERROR")
hoffm
  • 2,386
  • 23
  • 36
dragon
  • 9
  • 2
  • 3
  • yes ("An exception has occurred, use %tb to see the full traceback. SystemExit: ERROR") – dragon Oct 19 '17 at 14:21
  • that code is not even valid, please fix it. – norok2 Oct 19 '17 at 14:27
  • Which python version and how do you run the code ? – bruno desthuilliers Oct 19 '17 at 14:35
  • python 3.6 and spyder 3.1.4 editor – dragon Oct 19 '17 at 14:38
  • I think this question is fine: I agree it's a little unintuitive that the "system exit" function may spit out an exception, implying (to someone who doesn't know how it works) that something went wrong, even when the exit was perfectly fine – en_Knight Oct 19 '17 at 14:38
  • Also: several people have written answers to the effect that you should properly indent your code. If your original code was properly indented, please updated the question. That isn't an edit someone else can really make for you – en_Knight Oct 19 '17 at 14:51

4 Answers4

1

You have to indent correctly:

if not (userResponse =="n" or userResponse =="y") :
    exit('Error')

You could also raise an exception to get an even more descriptive error message:

userResponse = input ("n or y?")
if not (userResponse =="n" or userResponse =="y") :
    raise ValueError('Does not work')
mrCarnivore
  • 4,638
  • 2
  • 12
  • 29
  • Extended my answer. – mrCarnivore Oct 19 '17 at 14:27
  • I tried writing 'exit("ERROR")' in my python terminal and it worked fine. When does it need to be an integer? (I also think leaving it blank would work) – en_Knight Oct 19 '17 at 14:29
  • this a link to a picture of the error massage i still get https://e.top4top.net/p_657e40e81.png – dragon Oct 19 '17 at 14:33
  • @jooj add that information to your question (not as a picture) and you'll get better answers. @ mrCarnivore from the docs "sys.exit("some error message") is a quick way to exit a program when an error occurs" – en_Knight Oct 19 '17 at 14:35
  • @mrCarnivore : From the builtin help : exit(...) exit([status]) Exit the interpreter by raising SystemExit(status). If the status is omitted or None, it defaults to zero (i.e., success). If the status is an integer, it will be used as the system exit status. If it is another kind of object, it will be printed and the system exit status will be one (i.e., failure). – bruno desthuilliers Oct 19 '17 at 14:36
  • @brunodesthuilliers: You are right. But what is the question then? Was the problem just the missing indentation? – mrCarnivore Oct 19 '17 at 14:38
  • @mrCarnivore maybe you should have asked for clarifications in a comment instead of jumping in posting a at least partly incorrect answer ? – bruno desthuilliers Oct 19 '17 at 14:39
  • @brunodesthuilliers: Point taken. I will think about it more next time! It's hard to take your time in answering here, though. But once you write an elaborate answer there are 5 other shorter ones... – mrCarnivore Oct 19 '17 at 14:40
  • That's totally fair mrCarnivore, didn't mean to jump on you, and the question was a little unclear. You're answer, imo, helped clarify what the real probelm was due to what jooj posted in the comments from your suggestion – en_Knight Oct 19 '17 at 14:42
  • I updated the answer to correct it and highlight what the solution to the real problem was. – mrCarnivore Oct 19 '17 at 14:45
  • @mrCarnivore posting adequates answers is more important than being the first to answer. – bruno desthuilliers Oct 19 '17 at 14:46
1

Exit raises a SystemExit exception; that's how it terminates the program. That your environment displays that exception after terminating is not a bug, it's just showing you the cause. I agree with you (as does, e.g., this PEP), that the default behaviour should be to not print out a traceback message for this exception, and for KeyboardException

But in the end, the execution halted, as it was designed to do.

See the docs for details: https://docs.python.org/2/library/sys.html#sys.exit


A few answers here initially indicated that you cannot pass a string to exit. That is false: from the docs (emphasis mine)

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. Most systems require it to be in the range 0–127, and produce undefined results otherwise.... If another type of object is passed, None is equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1. In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.


The builtin exit function does the same thing. In general, it is not great to shadow the builtin function by importing from a module, though in this case it doesn't matter. Recommended style would be

import sys
sys.exit("My exception")

If this seems unintuitive to you, you can bypass the exception handling by using os._exit. Don't do these without cause

en_Knight
  • 5,301
  • 2
  • 26
  • 46
1

I was running into issues while trying to exit a script without raising an exception. What worked best for my case was os._exit(exitcode).

Ivaylo Novakov
  • 775
  • 6
  • 18
0

Following up on @MrCarnivore...

from sys import exit

userResponse = raw_input("n or y?")
if not (userResponse =="n" or userResponse =="y"):
     print('Error Message Here')
     exit(1)
cer
  • 1,961
  • 2
  • 17
  • 26
  • i get this error massage :(Error An exception has occurred, use %tb to see the full traceback. SystemExit: 1 /Users/juhina/anaconda/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2889: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D. warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1) – dragon Oct 19 '17 at 14:29
  • When does exit require an integer? On my interactive console, 2.7, it works fine with "exit('MESSAGE')" – en_Knight Oct 19 '17 at 14:31
  • @cer : """exit(...) exit([status]) Exit the interpreter by raising SystemExit(status). If the status is omitted or None, it defaults to zero (i.e., success). If the status is an integer, it will be used as the system exit status. If it is another kind of object, it will be printed and the system exit status will be one (i.e., failure).""" - IOW passing a message is perfectly legal. Also if you want to print errors, print them on `sys.stderr`, `sys.stdout` is for normal program outputs. – bruno desthuilliers Oct 19 '17 at 14:36
  • @jooj you are confused because you are running this from ipython, which is catching the SystemExit exception for you – avigil Oct 19 '17 at 14:49