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 programMy 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?
6 Answers
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() 2 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() 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 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_overAbove 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.
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.
import sys
and place sys.exit() where you want to stop.
1