Why is sys.exit() action order randomized?


#1
from sys import exit
print("ess")
print("ket")
print("it")
exit("derose")

Okay so I tried to run this code here in PyCharm and it seems to put the exit(message) above all the prints, it went under all of them only once out of 100 tries, it went to the 3rd position three times and to the second position about 17 times. Why is this random? I want an exit that is always ending the program, how can the program still print stuff when I’ve exited it already. My aesthetics aren’t calm and it doesn’t let me sleep, please help.


#2

Watch out with the term “random” around mathematicians, programmers etc - we expect some properties out of something that is random. Can it be used as a fair dice roll? If not, definitely not random!

Those strings are written in correct order, but they’re written to separate streams (stdout and stderr)
Figuring out the order of messages of both streams isn’t super easy. Terminals seem to be able to do it “correctly” but I don’t know whether it’s really “correct” or just sufficiently fast or what they do differently.

Python exits when it reads past your last line of code, usually that’s how you want to terminate. sys.exit can be used to indicate failure to whatever ran the program.


#3

Well, what can I use to completely stop the program then. sys.exit seems to be working fine at that, the only problem is that it’s shown in a weird order in the Run window.


#4

You don’t need to do anything. When your code has executed, the program stops.


#5

But I want my code to run into a wall. I need to stop it somewhere in between the code, that’s why I use sys.exit which works fine but as I said it’s showing weirdly in the Run window. I need it for my program, person can fail here by typing wrong stuff into the input, that’s what I need to do. I don’t feel like you’re getting me at all.


#6

Validating user input should not involve raising exceptions, which is what sys.exit involves. Use try: except: final: to work toward your ends, and cycle the user back through the input phase until satisfactory inputs are received.


#7

Me getting you is a result of you cluing me in.
You asked how to exit, and that’s the normal way to exit.
sys.exit doesn’t need to behave like print, print behaves like print.
You can use sys.exit to signal abnormal and fatal exits, it is suitable for that.

Like @mtf points out, interactive programs should probably not be freaking out quite that much on bad input, and instead ask again. Python itself is an example of this, when you run it in interactive mode and write something that’s invalid syntax or crashes, it doesn’t stop the whole program, it instead asks for another instruction. If you open a terminal and type in some nonsense, it too won’t shut down, it’ll ask again.
An alternative is to make it non-interactive, to let it take input when it is started, and then it would be appropriate to freak out if the input makes no sense.


#8

Alright matey, thanks for the help. I don’t what these do but I will look it up.


#9

I sounded a bit aggressive, sorry for that. I see I was in the wrong here, thanks for your patience bro.