54

For example:

def main():
    if something == True:
        player()
    elif something_else == True:
        computer()

def player():
    # do something here
    check_winner()  # check something 
    computer()  # let the computer do something

def check_winner(): 
    check something
    if someone wins:
        end()   


def computer():
    # do something here
    check_winner() # check something
    player() # go back to player function


def end():
    if condition:
        # the player wants to play again:
        main()
    elif not condition:
        # the player doesn't want to play again:
        # stop the program

     
    # whatever i do here won't matter because it will go back to player() or computer()

main()  # start the program

My problem is that if a certain condition becomes true (in the function check_winner) and function end() executes it will go back to computer() or player() because there's no line that tells the computer to stop executing player() or computer(). How do you stop functions in Python?

Georgy
  • 12,464
  • 7
  • 65
  • 73
user3711671
  • 823
  • 1
  • 6
  • 13
  • 9
    `return` terminates a function, is that what are you looking for? – Maroun Jan 06 '15 at 15:55
  • Yes (as long as it can be used again without restarting the program). – user3711671 Jan 06 '15 at 16:01
  • I forgot to add 1 function (main) in the question, but I still have a problem, like others said return returns to a function that called the 2nd function (the one with return statement) so it will never stop, adding return to several places didn't do anything and the program will still execute, where exactly do I need to write return?I want the program to go back to main() if the player wants to and if he doesn't I want to completely stop the program. – user3711671 Jan 07 '15 at 11:14

5 Answers5

76

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': 
        return
    second_test_variable = second_test()
    if second_test_variable == '1': 
        return
       
    # let the computer do something
    computer()
Georgy
  • 12,464
  • 7
  • 65
  • 73
Hektor
  • 1,845
  • 15
  • 19
  • So I just need to add 'return winner' (or just return (None)?) before end() in the function check_winner? – user3711671 Jan 06 '15 at 15:59
  • 1
    Yes,any point at which return is specified will terminate function execution and return to the calling code. – Hektor Jan 06 '15 at 16:01
11

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` 
    do_something_else()
Georgy
  • 12,464
  • 7
  • 65
  • 73
jez
  • 14,867
  • 5
  • 37
  • 64
6

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:
        main()
    elif not condition:
        sys.exit("The player doesn't want to play again") #Right here 
adriancm93
  • 83
  • 2
  • 5
3
def player(game_over):
    do something here
    game_over = check_winner() #Here we tell check_winner to run and tell us what game_over should be, either true or false
    if not game_over: 
        computer(game_over)  #We are only going to do this if check_winner comes back as False

def check_winner(): 
    check something
    #here needs to be an if / then statement deciding if the game is over, return True if over, false if not
    if score == 100:
        return True
    else:
        return False

def computer(game_over):
    do something here
    game_over = check_winner() #Here we tell check_winner to run and tell us what game_over should be, either true or false
    if not game_over:
        player(game_over) #We are only going to do this if check_winner comes back as False

game_over = False   #We need a variable to hold wether the game is over or not, we'll start it out being false.
player(game_over)   #Start your loops, sending in the status of game_over

Above is a pretty simple example... I made up a statement for check_winner using score = 100 to denote the game being over.

You will want to use similar method of passing score into check_winner, using game_over = check_winner(score). Then you can create a score at the beginning of your program and pass it through to computer and player just like game_over is being handled.

GeoStoneMarten
  • 533
  • 1
  • 7
  • 19
MrAlexBailey
  • 5,219
  • 19
  • 30
  • Since 'if' ends with a 'return' you do not need to add an 'else'. Just 'return false' is enough after the content of the if statement. (this has to be outside the scope of the if statement) – Nht_e0 May 18 '20 at 03:02
1

Maybe you are looking yield, its same as return but it stops function's execution rather than terminating functions, You can look at generators here.