By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,270 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,270 IT Pros & Developers. It's quick & easy.

Stopping a program

P: n/a
I'd like to stop a program (i.e. terminate its execution) without
raising an exception if some condition is met, e.g

answer = " "
while answer not in "yn":
answer = raw_input("y for yes, n for no, enter to exit ")
if answer == ""
stop/quit/end/whatever it takes to terminate the program
elif answer == "y"
#Yes
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 2004-02-16, Thomas Philips <tk****@hotmail.com> wrote:


I'd like to stop a program (i.e. terminate its execution) without
raising an exception if some condition is met, e.g


sys.exit(RETURN_CODE)

(Where RETURN_CODE is usually 0)
Jul 18 '05 #2

P: n/a
A follow up to the question posed above: I discovered sys.exit() and
played around it. I find that it exhibits different behaviors
depending on whether the program is run from IDLE or fron the command
line. In idle, calling sys.exit() gives me a barrage of output that
starts with
Traceback (most recent call lat):
and ends with
SystemExit: 0

after which I get back to the prompt. However, if the program is run
from the command line, the window with the command prompt simply
disappears (most likely after the same barrage flashes across it to
fast for the eye to follow).

Is there a variant of sys.exit() that will exit the program gracefully
to the command prompt in IDLE without bombarding me with information
that I know to be irrelevant?

Thomas Philips
Jul 18 '05 #3

P: n/a
tk****@hotmail.com (Thomas Philips) writes:
A follow up to the question posed above: I discovered sys.exit() and
played around it. I find that it exhibits different behaviors
depending on whether the program is run from IDLE or fron the command
line. In idle, calling sys.exit() gives me a barrage of output that
starts with
Traceback (most recent call lat):
and ends with
SystemExit: 0
IDLE is designed not to exit when SystemExit is raised. Currently,
the implementation deliberately shows the exception, rather than just
returning to the command prompt.
after which I get back to the prompt. However, if the program is run
from the command line, the window with the command prompt simply
disappears (most likely after the same barrage flashes across it to
fast for the eye to follow).
It just exits.
Is there a variant of sys.exit() that will exit the program
gracefully to the command prompt in IDLE without bombarding me with
information that I know to be irrelevant?

answer = " "
while answer not in "yn": answer = raw_input("y for yes, n for no, enter to exit: ")
if answer == "":
break
elif answer == "y":
print 'y'
elif answer == "n":
print 'n'
y for yes, n for no, enter to exit: a
y for yes, n for no, enter to exit:


Just use 'continue', 'break', or 'pass'. Inside a function, if there
is nothing more to do, use 'return', it's clearer.

--
KBK
Jul 18 '05 #4

P: n/a
Hello Thomas,
I'd like to stop a program (i.e. terminate its execution) without
raising an exception if some condition is met, e.g

http://www.python.org/doc/current/lib/module-sys.html, see `exit'

HTH.
Miki
Jul 18 '05 #5

P: n/a
Hello Thomas,
A follow up to the question posed above: I discovered sys.exit() and
played around it. I find that it exhibits different behaviors
depending on whether the program is run from IDLE or fron the command
line.

sys.exit just raises SystemExit exception.
IDLE catches this exception and shows the traceback as it does to
every other exception (try `raise OSError' in IDLE).
In the command line raising SystemExit will quit the interpreter. If
you just click on the .py file from the explorer it will close after
executing the program. If you want to view what happened add
`raw_input()' just before raising SystemExit.

For most cases raising SystemExit does what you want. Just remember
the IDLE does not emulate the command prompt.

HTH.
Miki
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.