Whats is the function of exit()?


I’m in the process of learning Python. And I have some questions that I do not completely understand after researching.

  1. What does the “exit()” function? Because if I run a basic program, it does not matter if I typed or not the “exit()” function. The program stops:


exit() exits the program. You can also exit the program just by
getting to the end: when there is no more code to run, the program
stops. But exit() let’s you exit before reaching the end of the

Save this code in a file “coin.py” and run it to see how it works:

import random
print("Coin tossing game")

while True:
    if random.random() < 0.5:
        print("Tails you lose.")
    print("Congratulations, you win!")
    answer = input("Would you like to play again? (y/n) ")
    if answer != 'y':

print("Thank you for playing. Goodbye!")

This program uses two different ways to exit. If you get Tails, the
program uses exit() to immediately exit completely. If you get Heads,
the program asks if you want to play again, and if you don’t enter ‘y’
then first it breaks out of the loop, then prints a goodbye message, and
then exits at the end of the program.

1 Like

Hello, Steven.

I read and run the code that you shared and I understand it clearly.

Thanks for your answer and time!

Note that exit() isn’t necessarily defined. The interpreter may be executed with the -S command-line option, which prevents automatically importing the site module, which is what adds exit() to the builtins scope. The reliable alternative is to import the sys module and call sys.exit(), or execute the statement raise SystemExit.

Mmm ok. I didn’t know that. I’m using PyCharm. When I used the “exit()” function it worked, without importing. But I don´t know if your comment is related to the the IDLE Python, because I really do not sure what " The interpreter may be executed with the -S command-line option" means.

But thanks, Eryk. I will keep it in mind.


A process in Windows has a process environment block (PEB) that includes process parameters such as a command-line string, environment variables, and standard I/O handles. Classically, a user runs a program with a command-line interface (CLI) shell such as CMD or PowerShell, and the process command line corresponds, for the most part, to the line that the user enters at the shell prompt. For example, python -S "path\to\script.py". Python has many command-line options. You can show them in the console/terminal via python -h. (If the shell can’t find “python”, try “py” instead.)

Note that in Unix the details are a bit different. A Unix process has an argument list (array) instead of a command-line string. To a user it seems roughly the same when running a program with a CLI shell such as bash. The difference under the hood is that the shell parses the command line into an argument list instead of simply passing the unparsed command-line string to the application. The Unix approach is more reliable and consistent for users.