Reputation: 853
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?
Upvotes: 57
Views: 391075
Reputation: 1
To exit a program/function, use the built-in exit()
function, and to exit a loop, use break
.
Upvotes: -1
Reputation:
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.
Upvotes: 1
Reputation: 15369
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()
Upvotes: 12
Reputation: 1945
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()
Upvotes: 86
Reputation: 93
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
Upvotes: 7
Reputation: 5289
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.
Upvotes: 3