31

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.

14
  • 14
    all those should work? Commented Jun 18, 2013 at 21:51
  • 7
    Can you show us the code you're using where this doesn't work? Are you trying to exit the program, or just the if statement? Are you catching SystemExit for some reason? Commented Jun 18, 2013 at 21:52
  • 2
    The only cases in which this could possibly fail are (a) not actually calling exit, (b) catching SystemExit or BaseException, (c) multithreading or multiprocessing, (d) really funky signal handling, or (e) embedded/custom Python interpreter. I strongly suspect it's (a). But if you don't give us your code (or, better, an SSCCE that minimally shows the same problem), we can't do more than suspect and guess. Commented Jun 18, 2013 at 21:59
  • 4
    @ethan: It is impossible to format code in comments on SO. That makes it very hard to read (and also makes it impossible to diagnose indentation errors). Just edit your question and paste the properly-formatted code there. Also, please describe the actual input/output sequence that you're seeing. (For example, if you type "N", see "ok, sayonnara", but then don't exit, tell us exactly that.) Commented Jun 18, 2013 at 22:00
  • 1
    @ChristianCareaga: I understand your frustration, but really, there's no harm here to anyone but the OP himself. If he never edits the question to become answerable, it'll just be closed (either in the present, or months later when someone searches for the same problem and finds a dead and useless question). Commented Jun 18, 2013 at 23:15

1 Answer 1

48

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

Sign up to request clarification or add additional context in comments.

11 Comments

already set up something similar and it didn't work. this is the code. if answer.lower().startswith("y"): print("ok, carry on then") elif answer.lower().startswith("n"): print("ok, sayonnara") sys.exit()
@ethan This solution is fair good enough. Paste your exact code here.
I'd be very surprised if this actually works for you in Python 3.2. When I try it, I get the expected NameError.
@tkoomzaaskz: Yes, I'm nearly 100% sure this code works. Sadly, I'm also 95% sure that it's not the OP's code (even though he implied that it was), or he wouldn't have had this question in the first place…
It's recommended to use import sys and then sys.exit() instead of the built-in exit(). See official docs and Difference between exit() and sys.exit() in Python.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.