Stopping a program

T

Thomas Philips

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
.
.
elif answer == "n"
#No
.
.

In Fortran, I'd use a stop command to do this. What is the appropriate
equivalent in Python?

Thomas Philips
 
T

Tyler Eaves

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)
 
T

Thomas Philips

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
 
K

Kurt B. Kaiser

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 = 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.
 
M

Miki Tebeka

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,513
Latest member
JeremyLabo

Latest Threads

Top