how to exit a python script in an if statement

0 votes
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,100 points
293,174 views

3 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,530 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!!
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()

 

answered Dec 14, 2020 by Rajiv
• 8,910 points
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,520 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,100 points
22,110 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,810 points
2,205 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
724 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
951 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 3,270 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,530 points
95,271 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,530 points
491 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