How to exit a python script in an if statement
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
To stop code execution in Python you first need to import the sys object. After this, you can then call the exit() method to stop the program from running.
You can follow this:
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()
edit: use input in python 3.2 instead of raw_input