[Tutor] Use flag to exit?

Alan Gauld alan.gauld at btinternet.com
Thu Jun 24 19:13:04 CEST 2010


"Richard D. Moores" <rdmoores at gmail.com> wrote

> How can I prevent
>
> Traceback (most recent call last):
>  File "C:/P31Working/prime_to_biggest_prime_tutor3.py", line 45, in 
> <module>
>    sys.exit()
> SystemExit
>
> from printing? Or isn't using sys.exit() a good idea?

Steven has already given you an answer to that although
I'd add that I have no problems using sys.exit() in top level code.
Just don't use it inside a function. Either raise an exception
or return a value.

If you are using it at the top level you may be running your code
inside an IDE like IDLE or Pythonwin which always catches
sys.exit(). That's OK, it's intended to print that message rather
than exit so that you can debug the code if necessary, but it
won't do it if you run the code from the OS prompt it will just
exit silently as expected.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list