簡體   English   中英

Python:中斷函數的執行

[英]Python: Interrupting the execution of a function

我是Python的新手,正在嘗試構建井字游戲。 我想互聯網上有很多解決方案,但是我想嘗試一下而不看它們。 現在中途我遇到了一個問題:當我輸入一個尚未占用的職位時,函數def player_input(board)按預期工作。 但是,當那里已經有一個標記“ X”或“ O”時,我想if board[marker_pos] == "X" or board[marker_pos] == "O":抓住它並重新啟動功能,所以用戶再次看到輸入字段。

這樣做是可以的,但是正如您在下面的兩張圖片中所見,當我嘗試將標記放在一個已經占用的字段中時,該功能似乎已完全執行。 我以為當我從自身(player_input(board) )內部調用函數時,它會被中斷。 因此它向我顯示了display_board(board)的結果,並print "iTurn is %s " %iTurn兩次print "iTurn is %s " %iTurn ,或者多次嘗試使我將標記置於空白位置。

第一輸入 第二和第三輸入

from IPython.display import clear_output

    def intro():
        board = [0]*9
        print "Player1, your marker is 'X'."
        print "Player2, your marker is 'O'."
        global iTurn
        iTurn = 1 

    def display_board(board):
        print "    |   |"
        print "  " + str(board[6]) + " | "  + str(board[7]) + " | "  + str(board[8]) 
        print "-------------"
        print "  " + str(board[3]) + " | "  + str(board[4]) + " | "  + str(board[5]) 
        print "-------------"
        print "  " + str(board[0]) + " | "  + str(board[1]) + " | "  + str(board[2]) 
        print "    |   |"


    def player_input(board):
        global iTurn
        if iTurn%2 != 0:
            marker_pos = input("Player1, please write which position (1 through 9) your marker should go")
        else:
            marker_pos = input("Player2, please write which position (1 through 9) your marker should go")     
        marker_pos -= 1

        if marker_pos < 0 or marker_pos > 8:
            print "Position needs to be between 1 and 9."
            player_input(board)

        else:
            if  board[marker_pos] == "X" or board[marker_pos] == "O":
                print "Position is already occupied."
                player_input(board)
            elif iTurn%2 != 0:
                board[marker_pos] = "X"
            else:
                board[marker_pos] = "O"    
        iTurn += 1
        print "iTurn is %s " %iTurn
        #check_winning(board)
        display_board(board)

編輯:這將是while循環的代碼

while (marker_pos < 0 or marker_pos > 8) or (board[marker_pos] == "X" or board[marker_pos] == "O"):
    if (marker_pos < 0 or marker_pos > 8):
        print "Position needs to be between 1 and 9."
    elif (board[marker_pos] == "X" or board[marker_pos] == "O"):
        print "Position is already occupied."
    marker_pos = input("Please write where your marker should go (1 through 9).")
    marker_pos -= 1

我認為問題是,您不退出位置無效的函數。 這就是為什么-在運行具有有效位置的player_input之后-之前調用的那個也會運行最后一個命令。 如果您只是在再次調用player_input之后返回就可以解決此問題。

player_input(board)更改為

return player_input(board)

要么

player_input(board)
return

您可以使用while True重復它並break以退出循環

def player_input(board):
    global iTurn

    while True:
        if iTurn%2 != 0:
            marker_pos = input("Player1, please write which position (1 through 9) your marker should go")
        else:
            marker_pos = input("Player2, please write which position (1 through 9) your marker should go")     
        marker_pos -= 1

        if marker_pos < 0 or marker_pos > 8:
            print "Position needs to be between 1 and 9."
        else:
            if  board[marker_pos] == "X" or board[marker_pos] == "O":
                print "Position is already occupied."
            elif iTurn%2 != 0:
                board[marker_pos] = "X"
                break
            else:
                board[marker_pos] = "O"
                break

    iTurn += 1
    print "iTurn is %s " %iTurn
    display_board(board)

您可以寫得更簡單

def player_input(board):
    global iTurn

    if iTurn%2 != 0:
        text = "Player1, please write which position (1 through 9) your marker should go"
        mark = "X"
    else:
        text = "Player2, please write which position (1 through 9) your marker should go"        
        mark = "O"

    while True:
        marker_pos = input(text)
        marker_pos -= 1

        if marker_pos < 0 or marker_pos > 8:
            print "Position needs to be between 1 and 9."
        else:
            if board[marker_pos] != 0:
                print "Position is already occupied."
            else:
                board[marker_pos] = mark
                break

    iTurn += 1
    print "iTurn is %s " % iTurn
    display_board(board)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM