I have a python program that takes integer only inputs like this:
Valid = False
while not Valid:
try:
x = int(Input("enter integer value"))
Valid = True
except:
pass
For easier debugging, I want to be able to terminate the program while it is running by giving an exit command as an input, rather than by killing the terminal, so I wrote this function:
def Input(message):
x = input(message)
if x == "exit":
#something to terminate program
return x
By my understanding, common options like sys.exit()
and os._exit()
work by raising a SystemExit
exception so they are ignored.
Is there a more definite way of terminating a program without raising an exception?
Nathan Sloley is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
If I understood correctly, os._exit()
doesn’t raise a SystemExit
exception, have you tried using that?
whereas sys.exit()
, as you rightly stated raises and exception.
divine architect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.