python – How to stop a function

python – How to stop a function

A simple return statement will stop or return the function; in precise terms, it returns function execution to the point at which the function was called – the function is terminated without further action.

That means you could have a number of places throughout your function where it might return.
Like this:

def player():
    # do something here
    check_winner_variable = check_winner()  # check something 
    if check_winner_variable == 1: 
    second_test_variable = second_test()
    if second_test_variable == 1: 
    # let the computer do something

In this example, the line do_something_else() will not be executed if do_not_continue is True. Control will return, instead, to whichever function called some_function.

def some_function():
    if do_not_continue:
        return  # implicitly, this is the same as saying `return None` 

python – How to stop a function

This will end the function, and you can even customize the Error message:

import sys

def end():
    if condition:
        # the player wants to play again:
    elif not condition:
        sys.exit(The player doesnt want to play again) #Right here 

Leave a Reply

Your email address will not be published. Required fields are marked *