Stopping a program

Kurt B. Kaiser kbk at shore.net
Sun Feb 15 23:31:37 EST 2004


tkpmep at 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



More information about the Python-list mailing list