473,394 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Stopping a program

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
5 2262
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: BKM | last post by:
I'm using a VB6 WebBrowser control to get info from various web pages and, occasionally, my program stops when it finds a script error on the page. It won't resume until I click 'Yes' or 'No' on...
8
by: Toni | last post by:
Hello I'm a newbie in VB .NET and I'd like to accomplish a (I think) very simple task. What I want to do is throw an error message to the user whenever something happens. The main point is...
2
by: UJ | last post by:
I've got a program that will do some updates to the system. During that time, if a certain program, called a.exe is running, I want to stop the program, do what I need to do and restart the...
4
by: bjm | last post by:
I am writing a program that will automate a series of application installations. I want to give the user the option of stopping the program's execution in between installations (for example, give...
4
by: Jeff | last post by:
Hi, I'd like to write Javascript that stops animated gifs from animating. On Firefox, at least, window.stop(); does the trick, although it stops everything on the page and is kind of...
10
by: kkrish | last post by:
Hello all, I am using MSDOS operating system and in a function of a program I tried to display a few hundred lines of a file in graphics mode using outtextxy(). The program gets 20 lines from...
3
by: grace | last post by:
dear everyone, we installed a mysql vr standard-4.11.22a-pc on a machine with ubuntu breezy badger as os..after the installation we are trying to start the server by typing :bin/safe_mysqld...but...
6
by: Ed Bitzer | last post by:
You have taught me how to start another program using System.Diagnostics.Process.Start and suggested a good tip site and Google search for future reference. My problem now is how to stop that...
2
daniel aristidou
by: daniel aristidou | last post by:
Hi i wrote code to print records off a datagrid.the code works on all but one of my data grids. The problem is that loop continues without stopping, Causing the program to crash. The only diff...
2
by: Steve | last post by:
Hi All, I've been trying to come up with a good way to run a certain process at a timed interval (say every 5 mins) using the SLEEP command and a semaphore flag. The basic thread loop was always...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.