Is there a method that tells my program to quit?

后端 未结 5 1132
不知归路
不知归路 2020-12-25 10:21

For the \"q\" (quit) option in my program menu, I have the following code:

elif choice == \"q\":
    print()

That worked all right until I

5条回答
  •  没有蜡笔的小新
    2020-12-25 11:13

    The actual way to end a program, is to call

    raise SystemExit
    

    It's what sys.exit does, anyway.

    A plain SystemExit, or with None as a single argument, sets the process' exit code to zero. Any non-integer exception value (raise SystemExit("some message")) prints the exception value to sys.stderr and sets the exit code to 1. An integer value sets the process' exit code to the value:

    $ python -c "raise SystemExit(4)"; echo $?
    4
    

提交回复
热议问题