How To Exit A Python Script In An If Statement | Edureka Community

21051/how-to-exit-a-python-script-in-an-if-statement

  • Home
  • Community
  • Categories
  • Python
  • how to exit a python script in an if statement
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.
  • python
  • python-3-x
Sep 19, 2018 in Python by Priyaj 58,020 points 362,033 views answer comment
  • flag

Your comment on this question:

Your name to display (optional):
Email me at this address if a comment is added after mine:
Privacy: Your email address will only be used for sending these notifications.
Add comment Cancel

5 answers to this question.

Your answer

Your name to display (optional):
Email me at this address if my answer is selected or commented on:
Privacy: Your email address will only be used for sending these notifications.
Add answer Cancel
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,520 points comment
  • flag
  • ask related question
How do I condition my program to exit? It usually closes the terminal window by itself after the program. commented Jul 20, 2019 by Ceefoon
  • flag
  • reply
Hi @Ceefon, did you try the above mentioned code? It worked fine for me. commented Jul 22, 2019 by Karan
  • flag
  • reply
Using Python 3.7.6, I get 'NameError: name 'exit' is not defined' commented Sep 14, 2020 by Nick
  • flag
  • reply

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.

commented Sep 14, 2020 by Gitika 65,730 points
  • flag
  • reply
import sys # to exit the code sys.exit() commented Sep 22, 2020 by MDG
  • flag
  • reply

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() commented Sep 22, 2020 by Gitika 65,730 points
  • flag
  • reply
This worked like dream for what I needed !!!!!!! Thank you!! commented Sep 26, 2020 by SexySpoiled87
  • flag
  • reply
Worked Like a charm Thanks a ton man this was a real life-saver commented Jun 17, 2021 by Max ImumOcUpency 120
  • flag
  • reply
Yes this works very well. Thanks Geetika commented Jul 7, 2021 by anonymous
  • flag
  • reply

Your comment on this answer:

Your name to display (optional):
Email me at this address if a comment is added after mine:
Privacy: Your email address will only be used for sending these notifications.
Add comment Cancel
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 comment
  • flag
  • ask related question
Works for me! Thanks commented May 3, 2021 by Palko
  • flag
  • reply
Thanks for this solution. It worked really well in my program commented Dec 13, 2021 by Paul C
  • flag
  • reply

Your comment on this answer:

Your name to display (optional):
Email me at this address if a comment is added after mine:
Privacy: Your email address will only be used for sending these notifications.
Add comment Cancel
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,440 points comment
  • flag
  • ask related question

Your comment on this answer:

Your name to display (optional):
Email me at this address if a comment is added after mine:
Privacy: Your email address will only be used for sending these notifications.
Add comment Cancel
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 comment
  • flag
  • ask related question

Your comment on this answer:

Your name to display (optional):
Email me at this address if a comment is added after mine:
Privacy: Your email address will only be used for sending these notifications.
Add comment Cancel
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 comment
  • flag
  • ask related question

Your comment on this answer:

Your name to display (optional):
Email me at this address if a comment is added after mine:
Privacy: Your email address will only be used for sending these notifications.
Add comment Cancel

Related Questions In Python

+1 vote 1 answer

How to check if a string is null in python

I want to check if any NULL ...READ MORE

answered Mar 9, 2022 in Python by satish edited Mar 5 24,389 views
  • python
  • python-programming
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,674 views
  • python
  • list
  • if-statement
  • for-loop
  • break
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 2,060 views
  • python-programming
  • python
  • python-if-else
  • python-flow-control
  • python-loops
+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 3,012 views
  • python
  • performance
  • profile
0 votes 1 answer

how do i change string to a list?

suppose you have a string with a ...READ MORE

answered May 21, 2019 in Python by Mohammad 3,230 points 5,110 views
  • python-string
  • python-datatypes
  • python
  • python-test-processing
  • python-services
  • python-list
  • python-functions
  • python-sequence-types
  • python-types
0 votes 2 answers

how can i randomly select items from a list?

You can also use the random library's ...READ MORE

answered Apr 9, 2020 in Python by Patrick 8,230 views
  • python-programming
  • python
  • python-list
  • python-datatypes
  • python-functions
  • python-sequence-types
  • python-types
+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 7,332 views
  • python-programming
  • python
  • python-list
  • python-datatypes
  • python-functions
  • python-sequence-types
  • python-types
0 votes 1 answer

how do i use the enumerate function inside a list?

Enumerate() method adds a counter to an ...READ MORE

answered Nov 16, 2021 in Python by Ruhan Siddiqui 5,937 views
  • python-list
  • python-datatypes
  • python
  • python-functions
  • python-sequence-types
  • python-types
  • python-programming
+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,520 points 126,231 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,520 points 2,291 views
  • python
  • python-programming

Recent in Python

  • Reading different format files from s3 having decoding issues using boto3 May 17, 2024
  • What is the algorithm to find the sum of prime numbers in the input in python Feb 22, 2024
  • How to Concatenate Lists in Python? Dec 22, 2023
  • Sum of prime numbers from m to n in python Dec 13, 2023
  • What does // means in Python? Dec 13, 2023
  • All categories
  • Generative AI Generative AI (1,454)
  • Power BI Power BI (1,316)
  • DevOps & Agile DevOps & Agile (4,138)
  • Data Science Data Science (100)
  • ChatGPT ChatGPT (30)
  • Cyber Security & Ethical Hacking Cyber Security & Ethical Hacking (1,057)
  • Data Analytics Data Analytics (1,266)
  • Cloud Computing Cloud Computing (4,053)
  • Machine Learning Machine Learning (337)
  • PMP PMP (1,069)
  • Python Python (3,488)
  • SalesForce SalesForce (201)
  • Selenium Selenium (1,624)
  • Software Testing Software Testing (58)
  • Tableau Tableau (608)
  • Web Development Web Development (3,972)
  • UI UX Design UI UX Design (24)
  • Java Java (1,358)
  • Azure Azure (157)
  • Database Database (858)
  • Big Data Hadoop Big Data Hadoop (1,907)
  • Blockchain Blockchain (1,673)
  • Digital Marketing Digital Marketing (121)
  • C# C# (141)
  • C++ C++ (272)
  • IoT (Internet of Things) IoT (Internet of Things) (390)
  • Kotlin Kotlin (8)
  • Linux Administration Linux Administration (389)
  • MicroStrategy MicroStrategy (7)
  • Mobile Development Mobile Development (395)
  • Others Others (2,386)
  • RPA RPA (653)
  • Talend Talend (73)
  • TypeSript TypeSript (124)
  • Apache Kafka Apache Kafka (84)
  • Apache Spark Apache Spark (596)
  • Career Counselling Career Counselling (1,091)
  • Events & Trending Topics Events & Trending Topics (28)
  • Ask us Anything! Ask us Anything! (71)

Join the world's most active Tech Community!

Welcome back to the World's most active Tech Community!

Sign up with Gmail Sign up with Facebook

OR

Password must have

At least 1 upper-case and 1 lower-case letter

Minimum 8 characters and Maximum 50 characters

SIGN UP Already have an Edureka Account? Login Forgot Password? LOGIN Don’t have edureka account? Sign Up resend ? Password must have

At least 1 upper-case and 1 lower-case letter

Minimum 8 characters and Maximum 50 characters

reset password Don’t have edureka account? Sign Up reset password Don’t have edureka account? Sign Up Send Code Don’t have edureka account? Sign Up

Subscribe to our Newsletter, and get personalized recommendations.

Google Sign up with Google facebook Signup with Facebook

Already have an account? Sign in.

webinar REGISTER FOR FREE WEBINAR X Years of Experience* Student 0-2 Years 2-5 Years 5-10 Years 10+ Years REGISTER NOW webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP "PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.

Tag » How To Exit Python In Terminal