Search code examples
pythonexit-code

Press ENTER to exit program in Python not working


Trying to get my code to exit on the press of the enter button and I'm running into a ValueError: invalid literal for int() with base 10: '', but it's telling me that my error is on the line simulations_num = int(simulations). Anybody have an idea?

simulations = input("How many times would you like to run the simulation? ")

# Invalid answer, system exit
if (not simulations) or int(simulations) <=0:    
   print("That is not a valid number of simulations.")
   print("Shutting down.")
   SystemExit

simulations_num = int(simulations)
#the rest of my code comes here

I've also attempted if simulations == '' to no avail. I have noticed that if simulations_num = int(simulations) isn't involved, the code works fine, but that part of the code (or something similar) is necessary for the rest of the code


Solution

  • You need to raise SystemExit(), or just use sys.exit() (which does the same thing).