Python: how to stop a function?

For instance:

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 some condition becomes true (in the check_winner function) and the end () function is executed, it will return to the computer () or player (), because there is no line that tells the computer to stop the player () or computer (). How do you stop functions in Python?

+5
source share
3 answers

A simple return statement will "stop" or return a function, in exact terms, "return" the execution of the function to the point at which the function was called - the function is terminated without further action.

This means that you can have several places in your function where it can 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(); 
+4
source
 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 wrote instructions for check_winner using score = 100 to denote a game.

You will need to use the same method of passing score to check_winner using game_over = check_winner(score) . You can then create an estimate at the beginning of your program and pass it to computer and player , how game_over processed.

+1
source

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

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

Source: https://habr.com/ru/post/1210486/


All Articles