JAVA

how to exit a python script in an if statement

LO Asked by Lokesh · 16-11-2022
0 upvotes 269 views 0 comments
The question
I am currently utilizing Python 3.2 and seeking a method to terminate the program after the user indicates they do not wish to continue. Is there a way to implement an exit command within an if statement inside a while loop? I have already attempted using exit(), sys.exit(), sys.quit(), quit(), and raise SystemExit.

5 answers

0
SH
Answered on 26-12-2022

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!


RA 22-03-2023

Indeed, this solution is highly effective. Thank you, Geetika.

MU 27-02-2023

This solution worked perfectly for my requirements! Thank you very much!

DE 21-02-2023

Greetings, @MDG, Merely importing sys will not suffice to make the exit function accessible in the global scope. You will need to implement the following:

VE 13-02-2023

Could you kindly share your code snippet? It would be beneficial for us to understand your specific objectives in order to assist you promptly.

VE 23-01-2023

While using Python 3.7.6, I encountered a 'NameError: name 'exit' is not defined.'

MA 13-01-2023

Hello @Ceefon, have you attempted the code mentioned earlier? It functioned correctly for me.

MA 30-12-2022

How can I configure my program to terminate properly? It typically closes the terminal window automatically after execution.

0
SH
Answered on 26-06-2023
Hi everyone,

I would like to present my solution to the aforementioned queries:

To halt code execution in Python, you must first import the sys module. Following this, you can invoke the exit() method to terminate the program's operation. This approach is the most dependable and universally applicable method for stopping code execution. Below is a straightforward example.

import sys
sys.exit()

If you are keen on enhancing your Python skills, I recommend enrolling in the Data Science with Python Course
0
SH
Answered on 23-07-2023
In Python, the break statement allows you to terminate a loop when a specific external condition occurs. Typically, you will place the break statement within the code block of your loop, often following a conditional if statement.
0
PA
Answered on 30-07-2023
As a newcomer to Linux, I understand the chmod binary format, but I am uncertain about the permissions assigned by the value 755. Could you please provide further clarification?
0
SH
Answered on 22-09-2023
Rather than utilizing the standard UTF-8 encoding, which is widely accepted in the industry, I converted the code I retrieved from a webpage into this alternative encoding.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session