8 AnswersNew Answer
But what is it for? When would I need it?
For example: a = int(input()) b = int(input ()) if(b!=0): print("a/b:",a/b) else: print("Error: You can't divide by zero. we will force to close the program, sorry...") sys.exit() # Do something if everything is ok...
Ok, good example, doesn't Python finish executing the program anyway? Why is it necessary to write "sys.exit()", to make it explicit? (Sorry for my insistence, but I can't find the answer, I guess it's too basic for Stackoverflow 😅)
Ahhh, ok, so I don't HAVE to use it, I CAN use if for whatever reason it's useful for my goal to exit the script, it's just another tool... Thank you Rolando for your answers and for your patience!!! I really appreciate it 👍😃
You don't need sys.exit(). Use this when you want to stop your script.
A slightly more technical example: You are from a Shell and want to run a Python script, but you need to know if everything went well. One way to know if everything was executed correctly is to see some value that your script returns. Not only can you go out with sys.exit (), you can also go out with a custom value like sys.exit (1) or sys.exit (999) and use that value returned by your program to do one or another action from your shell.
Rolando A. Rosales J. has said it all