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?

Chris
  • 362
  • 3
  • 20

4 Answers4

7

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.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • So `return` will stop the program – Chris Aug 21 '19 at 09:50
  • And does this mean that I can define other functions inside a different function? – Chris Aug 21 '19 at 09:52
  • @ChrisOliver `return` stops function `main()`, not program - but after main() there is no more code to run - so Python ends program. – furas Aug 21 '19 at 09:53
  • So is there a way to `return` outside function – Chris Aug 21 '19 at 09:53
  • 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. – BoarGules Aug 21 '19 at 09:54
  • @ChrisOliver you can't use `return` outside function. You should rather use if/else to skip some code when you don't want to run it and then program will end in natural way. – furas Aug 21 '19 at 09:55
  • *So is there a way to return outside function*. No, but this code is not doing that. Your main program (under the `if`) is calling `main()`, which is a function, and the code returns from that function. You can't put `return` in your main program but you can put it in any function that it calls. – BoarGules Aug 21 '19 at 19:29
1

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.

ItsMeTheBee
  • 353
  • 2
  • 20
  • 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?](https://stackoverflow.com/a/19747562/320437) – pabouk - Ukraine stay strong May 11 '22 at 17:11
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()
0

you can try the following code to terminate the program.

import sys
sys.exit()
Yashi Aggarwal
  • 407
  • 2
  • 6
  • When I try that this is what I get: Traceback (most recent call last): File "C:\Users\Chris\Desktop\Test.py", line 3, in sys.exit() SystemExit – Chris Aug 21 '19 at 09:49