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
.
Asked
Active
Viewed 2.7e+01k times
30
-
14all those should work? – Serial Jun 18 '13 at 21:51
-
`if True: exit()` works. – Youcha Jun 18 '13 at 21:52
-
7Can 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? – Brendan Long Jun 18 '13 at 21:52
-
Does your code ever fulfil the if condition the exit statement is in? – Ben Jun 18 '13 at 21:58
-
2The 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](http://sscce.org) that minimally shows the same problem), we can't do more than suspect and guess. – abarnert Jun 18 '13 at 21:59
-
im trying to exit the whole script, the code is: (newline)if answer.lower().startswith("y"): (newline) print("ok, carry on then") (newline) elif answer.lower().startswith("n"): (newline) print("ok, sayonnara") (newline) sys.exit() – ethan Jun 18 '13 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.) – abarnert Jun 18 '13 at 22:00
-
all of the things you tried will work but if youre running the code in the IDE they will just give an error but if you run it from the cmd it will exit it could be something else in youre code – Serial Jun 18 '13 at 22:02
-
@ethan post your code in the question body. – Ashwini Chaudhary Jun 18 '13 at 22:03
-
@ChristianCareaga: First, at least some of them won't work anywhere (there's no such thing as `sys.quit()`. Second, I don't know what IDE you use, but in IDLE, Eclipse/PyDev, and both of the major emacs modes, if your program exits normally the IDE doesn't give an error. – abarnert Jun 18 '13 at 22:04
-
@abarnert sorry tryed to write IDLE it just raises a exit i mean what im saying is in IDLE it wont actually close the window it will stop the program sorry – Serial Jun 18 '13 at 22:05
-
this is becoming way more difficult than it needs to be just post youre code in the question im sure one of us can easily help!! – Serial Jun 18 '13 at 22:08
-
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). – abarnert Jun 18 '13 at 23:15
-
1Duplicate: http://stackoverflow.com/q/73663/3345375 – jkdev Oct 23 '16 at 15:29
1 Answers
47
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

d512
- 32,267
- 28
- 81
- 107
-
1already 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 Jun 18 '13 at 21:55
-
1
-
I typed your code almost verbatim into my python interpreter and it worked fine. You should post your code. Maybe something else going on in your while loop? – d512 Jun 18 '13 at 22:00
-
@tkoomzaaskz: The OP said that doesn't work, so why would they accept it? – martineau Jun 18 '13 at 22:02
-
1I'd be very surprised if this actually works for you in Python 3.2. When I try it, I get the expected `NameError`. – abarnert Jun 18 '13 at 22:04
-
@abarnert change `raw_input` into `input` and it works fine. Is it what you meant? – ducin Jun 18 '13 at 22:08
-
yeah, thanks for pointing that out. Regardless of the mechanism for getting the input, the point is that the exit() function ends the program when the if statement is properly tested. – d512 Jun 18 '13 at 22:12
-
I'm 100% sure that your code works because it's so easy and so correct :) @abarnert - any problems with this code? – ducin Jun 18 '13 at 22:13
-
1@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… – abarnert Jun 18 '13 at 23:13
-
5It's recommended to use `import sys` and then `sys.exit()` instead of the built-in `exit()`. See [official docs](https://docs.python.org/2/library/constants.html#exit) and [Difference between exit() and sys.exit() in Python](http://stackoverflow.com/q/6501121/3345375). – jkdev Oct 23 '16 at 15:51
-