How to exit python script in if statement

I am using Python 3.2 and trying to exit it after user input that they donโ€™t want to continue, is there any code that will exit it in the if statement inside the while loop? I already tried to use exit() , sys.exit() , sys.quit() , quit() and raise SystemExit .

+17
source share
1 answer

This works great 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

+30
source

All Articles