Python:中断函数的执行

时间:2019-05-19 08:12:27

标签: python python-2.7

我是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的结果两次,或者经过多次尝试才使我将标记置于空白位置。

1st input 2nd and 3rd input

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

2 个答案:

答案 0 :(得分:1)

我认为问题是,您不退出位置无效的函数。这就是为什么-在运行具有有效位置的!(b < a)之后-之前调用的那个也会运行最后一个命令。 如果您只是在再次调用player_input之后返回就可以解决此问题。

player_input更改为

player_input(board)

return player_input(board)

答案 1 :(得分:1)

您可以使用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)