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中停止函數?
'return'終止函數,是你在找什麼? – Maroun
是(只要它可以在不重新啓動程序的情況下再次使用)。 – user3711671
我忘了在問題中添加1個函數(main),但是我仍然遇到問題,就像其他人說的那樣,return返回一個調用第二個函數的函數(帶有return語句的函數),所以它永遠不會停止,幾個地方沒有做任何事情,程序仍然會執行,我需要寫回報的地方是什麼?如果玩家想要,程序會返回main(),如果他不想,我會完全停止該程序。 – user3711671