how to exit a python script in an if statement

+1 vote
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.
Sep 19, 2018 in Python by Priyaj
• 58,020 points
355,414 views

5 answers to this question.

0 votes

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("ok, sayonnara")
      exit()

edit: use input in python 3.2 instead of raw_input

Hope it helps!!

If you need to know more about Python, It's recommended to join Python course today.

Thanks!

answered Sep 19, 2018 by bug_seeker
• 15,510 points
How do I condition my program to exit?

It usually closes the terminal window by itself after the program.
Hi @Ceefon, did you try the above mentioned code? It worked fine for me.
Using Python 3.7.6, I get 'NameError: name 'exit' is not defined'

Hi, @Nick,

Could you please post your code snippet here, what exactly are you trying to do? It will be helpful for us to resolve it ASAP.

import sys

# to exit the code

sys.exit()

Hi, @MDG,

Importing sys will not be enough to make exit live in the global scope.

You either need to do

from sys import exit
exit()
This worked like dream for what I needed !!!!!!! Thank you!!
Worked Like a charm

Thanks a ton man this was a real life-saver
Yes this works very well. Thanks Geetika
0 votes

Hey All,

Here is my solution to all the above doubt:

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. It is the most reliable, cross-platform way of stopping code execution. Here is a simple example.



  1. import sys
  2. sys.exit()

If you are interested in learning Python consider taking Data Science with Python Course.

answered Dec 14, 2020 by Rajiv
• 8,870 points
Works for me! Thanks
Thanks for this solution. It worked really well in my program
0 votes

In Python, the break statement provides you with the opportunity to exit out of a loop when an external condition is triggered. You'll put the break statement within the block of code under your loop statement, usually after a conditional if statement.

answered Dec 14, 2020 by Roshni
• 10,480 points
0 votes
I'm new to Linux, and while I'm familiar with the chmod binary format, I'm not sure what 755 sets the permission to. Please elaborate
answered Mar 25, 2023 by Kanye
• 140 points

reshown Dec 11, 2023 by Soumya
0 votes

Instead of using the normal UTF-8 encoding, which is the industry standard, I transformed the code that I extracted from a webpage into this encoding.

answered Jul 4, 2023 by bodymist
• 140 points

Related Questions In Python

+1 vote
1 answer

How to check if a string is null in python

Try this: if cookie and not cookie.isspace(): # the ...READ MORE

answered Aug 20, 2018 in Python by Priyaj
• 58,020 points
23,090 views
0 votes
1 answer

How to break for loop in an if statement

You can break out of each loop ...READ MORE

answered Nov 16, 2018 in Python by Jino
• 5,820 points
3,011 views
0 votes
1 answer

How to use nested if else statement in python?

The working of nested if-else is similar ...READ MORE

answered Nov 19, 2018 in Python by Nabarupa
1,259 views
+1 vote
1 answer

How to Profile a script in Python

Python includes a profiler called cProfile. It ...READ MORE

answered Nov 21, 2018 in Python by SDeb
• 13,300 points
1,870 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+2 votes
4 answers

How can I replace values with 'none' in a dataframe using pandas

Actually in later versions of pandas this ...READ MORE

answered Aug 13, 2018 in Python by bug_seeker
• 15,510 points
122,321 views
0 votes
1 answer

When to use %r instead of %s in Python? [duplicate]

The %s specifier converts the object using ...READ MORE

answered Aug 2, 2018 in Python by bug_seeker
• 15,510 points
1,164 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP