Python:如何停止一个函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27802270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:17:54  来源:igfitidea点击:

Python: How to stop a function?

pythonfunction

提问by user3711671

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?

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

回答by Hektor

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.

一个简单的 return 语句将“停止”或返回函数;准确地说,它将函数执行“返回”到函数被调用的点——函数在没有进一步操作的情况下终止。

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

回答by jez

In this example, the line do_something_else()will not be executed if do_not_continueis True. Control will return, instead, to whichever function called some_function.

在本例中,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()

回答by MrAlexBailey

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_winnerusing score = 100to denote the game being over.

以上是一个非常简单的例子......我做了一个语句check_winner使用score = 100来表示游戏过度。

You will want to use similar method of passing scoreinto 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 computerand playerjust like game_overis being handled.

您将要使用合格的类似的方法scorecheck_winner,使用game_over = check_winner(score)。然后你可以在你的程序开始时创建一个分数并将它传递给computer并且player就像game_over被处理一样。

回答by 52eagle LOG

I'm just going to do this

我只是要这样做

def function():
  while True:
    #code here

    break

Use "break" to stop the function.

使用“break”停止函数。

回答by adriancm93

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