5 answers
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!
This solution worked perfectly for my requirements! Thank you very much!
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:
Could you kindly share your code snippet? It would be beneficial for us to understand your specific objectives in order to assist you promptly.
While using Python 3.7.6, I encountered a 'NameError: name 'exit' is not defined.'
Hello @Ceefon, have you attempted the code mentioned earlier? It functioned correctly for me.
How can I configure my program to terminate properly? It typically closes the terminal window automatically after execution.
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
Indeed, this solution is highly effective. Thank you, Geetika.