2015-01-06 136 views
5

例如:Python:如何停止函數?

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)和功能()結束執行它會回到電腦()或播放(),因爲沒有線它告訴計算機停止執行player()或computer()。你如何在Python中停止函數?

+0

'return'終止函數,是你在找什麼? – Maroun

+0

是(只要它可以在不重新啓動程序的情況下再次使用)。 – user3711671

+0

我忘了在問題中添加1個函數(main),但是我仍然遇到問題,就像其他人說的那樣,return返回一個調用第二個函數的函數(帶有return語句的函數),所以它永遠不會停止,幾個地方沒有做任何事情,程序仍然會執行,我需要寫回報的地方是什麼?如果玩家想要,程序會返回main(),如果他不想,我會完全停止該程序。 – user3711671

回答

4

一個簡單的return語句會'停止'或返回函數,確切地說,它'函數執行'返回函數被調用的地方 - 函數終止而不需要進一步的操作。

這意味着你可以在你的函數中有多個地方可能返回。 像這樣:

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

所以我只需要在函數check_winner的end()之前添加'return winner'(或者只是返回(None)?)? – user3711671

+0

是的,指定返回的任何點將終止函數執行並返回到調用代碼。 – Hektor

0

在此示例中,線do_something_else()不會如果do_not_continueTrue執行。控制權將返回到稱爲some_function的任何函數。

def some_function(): 

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

    do_something_else() 
1
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_winner使用score = 100表示遊戲過度。

您需要使用類似的方法將score轉換爲check_winner,使用game_over = check_winner(score)。然後,您可以在程序開始時創建一個分數,並將其傳遞至computerplayer,就像處理game_over一樣。