How to exit a python script in an if statement

592    Asked by RutujaMishra in Python , Asked on Apr 1, 2021

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 tried same using exit(), sys.exit(), sys.quit(), quit(), and raise SystemExit.

Please answer how python exits if statement?


Answered by Rutuja Mishra

You can use the below-mentioned code help you exit if statement :

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("ok, sayonnara")
      exit()
You can also use input in python 3.2 instead of raw_input.

Your Answer

Interviews

Parent Categories