2

How do you quit or halt a python program without the error messages showing?

I have tried quit(), exit(), systemexit(), raise SystemExit, and others but they all seem to raise an error message saying the program has been halted. How do I get rid of this?

5
  • use try...except... Commented Aug 21, 2019 at 9:42
  • What do you mean by error? The default message of python exit is this Process finished with exit code 0 Commented Aug 21, 2019 at 9:45
  • build program in this way that it ends running without using any function. Commented Aug 21, 2019 at 9:46
  • add little more context to your question. I'm not clear reg your expectations. Commented Aug 21, 2019 at 9:49
  • when I use sys.exit() or exit() then it ends program without any error message in terminal on Linux. How do your run script ? Where do you run this script ?Maybe message is not from script but from program which runs script. Commented Aug 21, 2019 at 9:51

4 Answers 4

8

You are trying too hard. Write your program using the regular boilerplate:

def main():
    # your real code goes here
    return

if __name__ == "__main__":
    main()

and just return from function main. That will get you back to the if-clause, and execution will fall out the bottom of the program.

You can have as many return statements in main() as you like.

Sign up to request clarification or add additional context in comments.

7 Comments

So return will stop the program
And does this mean that I can define other functions inside a different function?
@ChrisOliver return stops function main(), not program - but after main() there is no more code to run - so Python ends program.
So is there a way to return outside function
You can define one function inside another, but that is not regular Python style. They don't have to be inside main. Just write modular functions that you call from main, at the same level.
|
2

You would need to handle the exit in your python program. For example:

def main():
    x = raw_input("Enter a value: ")
    if x == "a value":
        print("its alright")
    else:
        print("exit")
        exit(0)

Note: This works in python 2 because raw_input is included by default there but the concept is the same for both versions.

Output:

Enter a value: a
exit

Just out of curiousity: Why do you want to prevent the message? I prefer to see that my program has been closed because the user forced a system exit.

1 Comment

Please do not use quit() or exit() in your code. These functions are intended only for the interactive Python. See Python exit commands - why so many and when should each be used?
1

you can structure your program within a function then return when you wish to halt/end the program

ie

def foo():
    # your program here
    if we_want_to_halt:
        return

if __name__ == "__main__":
    foo()

Comments

-1

you can try the following code to terminate the program.

import sys
sys.exit()

1 Comment

When I try that this is what I get: Traceback (most recent call last): File "C:\Users\Chris\Desktop\Test.py", line 3, in <module> sys.exit() SystemExit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.