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!
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.
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.
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).