473,769 Members | 5,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2282
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
7280
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 the script error message box. However, if I open an IE6 browser outside the program and manually go to the same page I don't get the Script Error message box. I do get a yellow exclamation mark symbol with the message "Done, but with errors on the...
8
2054
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 that the message must stop the code execution, but it sholud not close the application nor show the classical exception window (the one with JIT compiler information).
2
1439
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 program. How can I find out if a.exe is currently running and stop it? TIA - UJ.
4
2043
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 the user the chance to stop the program after the second installation before it continues on to the third installation). However, I want the user to be able to start the installations and walk away as well, so I can't ask the user if he wants to...
4
6367
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 unpredictable. If I connect it to the onload event, sometimes only half the page will be displayed. Does the onload even fire before rendering? Does anyone know a reasonable way to accomplish my original goal of
10
3226
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 a file and displays them using outtextxy with colored letters .When the user gives a key(up arrow) input the old content will be erased (i drew the old text in background color) and new text will be shown fresh from the file.
3
9989
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 it shows us a msg whch states Starting mysqld daemon with databases from /usr/local/mysql- standard-4.11.22a-pc-linux-gnu-i686-glibc23/data followed by... 'STOPPING SERVER from pid file /usr/local/mysql-standard-4.11.22a-pc-
6
1454
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 program I started. Even if I close my main program this program remains listed in the Task Manager. I promise you I searched but using words like end, stop, close found me lots of information but not my specific need. Appreciate if you would help...
2
1523
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 between all of the grids is the query and the widows form it is on. I basicly just copied the form changing the SQL for the datasource. any way so here is the code i typed: Private Sub Print_Click() Dim msg As Variant Dim spaceaftercolumn1 As Variant...
2
3369
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 sitting in the sleep command and not able to be interrupted. When the time came to set the semaphore flag to false (stopping the thread), my program would have to wait up to the entire sleep time to break out of the loop. I have finally found...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9999
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.