Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to exit a python script in an if statement [closed]

I'm using Python 3.2 and trying to exit it after the user inputs that they don't want to continue, is there code that will exit it in an if statement inside a while loop? I've already tried using exit(), sys.exit(), sys.quit(), quit(), and raise SystemExit.

like image 961
ethan Avatar asked Jun 18 '13 21:06

ethan


People also ask

Is there an exit function in Python?

In python, we have an in-built quit() function which is used to exit a python program. When it encounters the quit() function in the system, it terminates the execution of the program completely.

How do you exit a command in Python?

Exit Programs With the quit() Function in Python This site module contains the quit() function,, which can be used to exit the program within an interpreter. The quit() function raises a SystemExit exception when executed; thus, this process exits our program.


1 Answers

This works fine for me:

while True:
   answer = input('Do you want to continue?:')
   if answer.lower().startswith("y"):
      print("ok, carry on then")
   elif answer.lower().startswith("n"):
      print("sayonara, Robocop")
      exit()

edit: use input in python 3.2 instead of raw_input

like image 170
d512 Avatar answered Oct 24 '22 07:10

d512