Ending a program in Python

I’m trying to teach myself Python. (This has come about as a result of something I posted here a few months ago, so if you were in that thread and you’re reading this, then I say to you, Thanks for the suggestions!)

The last programming experience I’ve had was over 10 years ago, using BASIC. Python’s not hard, but it’s very different than BASIC so I’m having to change the way I think in several ways. I like it alot.

I’ve looked through all the tutorials, both those that come with the Python interpreter and those I could find online, and one thing I can not figure out is how in the heck I can end a program. (Of course, it ends when it has no more instructions to run, but I am looking for a way to end it before it runs out of instructions.)

Is there an explicit command for this? Or a “trick” of some kind that I’m not seeing?

The best I can do right now is to put some garbage on a line where I want the program to end. This causes an error, which causes the thing to stop running. But that can’t be the right way.

I thought there might be a way to do it involving enclosing the whole module in a “While end == false:” block, then have end get set to “true” wherever I would like the program to end itself. But this won’t keep it from running the entirity of the module before ending. (And in the program I’m working on, the module does not finish running, anyway.) I could fix this with a complicated series of flags, but this can’t be the way. There must be something better.

There is probably some really easy answer to this. Apologies for all ignorance and dumbness!

-FrL-

I believe sys.exit() is what you’re looking for.

I bet that’s it. Thanks!

-Kris

You’re looking for sys.exit(int). The integer that you pass to sys.exit() isn’t all that important. Basically you can choose any you like and you probably won’t notice any difference.

Passing 0 to sys.exit() says that “I successfully completed.” Passing a non-zero value to sys.exit() says that “I couldn’t finish what I was supposed to do because of some error”. The exit value won’t make any difference whatsoever unless you test it from the shell. If that last sentence was total gibberish to you, then that means the value that you pass to sys.exit() won’t matter at all.

It all made sense except “test it from the shell.” I would be interested in knowing what that means.

-Kris

  1. sys.exit() as the others said.

  2. I am obligated to post this as it’s about python: xkcd: Python

The shell is, in Unix-type systems, the thing that you use to run programs by typing commands. When executing a command, a program (such as your Python program) will be run, and if you want to check if that program was successful, you can inspect the exit code to see if it is zero or nonzero. (When running programs manually, you’ll probably get a bunch of error messages if there’s a problem, but when writing shell scripts to automate many tasks, you want to be able to check the exit status of things to handle errors programatically.)

Exit codes are also important when you’re writing programs that fork() and must maintain subprocesses. The exit code from a subprocess lets the main program know whether the subprocess’s task was successfully completed.

But, as usual, the Perl one is superior. :smiley:

Got it.

I’ll probably never actually use this information, but I’m still going to ask: How do I check the exit status of a program that just terminated?

I’m running on Windows, using the standard windows Python package that you get by going to Python’s website.

-FrL-

Hehe.

“I just typed ‘import antigravity’!”

-FrL-

I have no idea how you do that in Windows, or if it’s even possible. In Unix, though, you’d type

echo $?

which would show 0 if the previous command or program exited successfully, or a non-zero number (usually 1) if the program exited unsuccessfully.

I thought religious witnessing wasn’t allowed except in Great Debates :stuck_out_tongue:

No, this is the religious witnessing strip.

If you’re running Python from the Windows command prompt (CMD.EXE) the exit status of the last command is stored in %ERRORLEVEL%. If you’re using a Bourne-like shell (e.g., Cygwin’s bash shell), as Anne Neville says, the status is in $?. In csh-like shells it’s in status (and sometimes in ? as well).

Of course the universe is open source and user editable.

Er… xkcd… xkcd…

cues four yorkshiremen sketch

Well, you could always have that military colonel stop by and say ‘Stop that. It’s silly.’

Well, not really. That was Graham Chapman, and he’s dead.

You’re saying he’s an ex-Python? He’s ceased to be?

:smiley:

You missed the followup.