Python:如何停止一个函数?

时间:2015-01-06 15:53:47

标签: python function

例如:

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

我的问题是,如果某个条件变为真(在函数check_winner中)并且函数end()执行它将返回到computer()或player(),因为没有告诉计算机的行停止执行player()或computer()。如何在Python中停止函数?

7 个答案:

答案 0 :(得分:17)

一个简单的退货声明将会停止'或返回功能;准确地说,它会返回'函数执行到调用函数的位置 - 函数终止而无需进一步操作。

这意味着您可以在整个函数中拥有许多可能返回的位置。 像这样:

  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();

答案 1 :(得分:2)

在此示例中,如果do_something_else()do_not_continue,则不会执行第True行。相反,控件将返回到名为some_function的任何函数。

 def some_function():

     if do_not_continue:
         return    # implicitly, this is the same as saying return None

     do_something_else()

答案 2 :(得分:2)

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

上面是一个非常简单的例子......我使用check_winnerscore = 100制作了一个声明来表示游戏结束。

您将需要使用类似的方法将score传递到check_winner,使用game_over = check_winner(score)。然后,您可以在计划开头创建一个分数,并将其传递给computerplayer,就像处理game_over一样。

答案 3 :(得分:1)

我要去做

def function():
  while True:
    #code here

    break

使用“ break”停止该功能。

答案 4 :(得分:1)

这将结束功能,您甚至可以自定义“错误”消息:

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 

答案 5 :(得分:0)

也许您正在寻找 yield,它与 return 相同,但它停止函数的执行而不是终止函数,You can look at generators here.

答案 6 :(得分:-1)

导入系统

并将 sys.exit() 放在您想要停止的位置。