简体   繁体   English

Python:中断函数的执行

[英]Python: Interrupting the execution of a function

I'm new to Python and trying to build a tic-tac-toe game. 我是Python的新手,正在尝试构建井字游戏。 I guess there are a ton of solutions on the internet, but I wanted to try it without looking at them. 我想互联网上有很多解决方案,但是我想尝试一下而不看它们。 Now halfway through I ran into a problem: The function def player_input(board) works as intended when I input a position that's not already taken. 现在中途我遇到了一个问题:当我输入一个尚未占用的职位时,函数def player_input(board)按预期工作。 However, when there is already a marker "X" or "O" there, I want if board[marker_pos] == "X" or board[marker_pos] == "O": to catch this and start the function again, so the user sees the input field again. 但是,当那里已经有一个标记“ X”或“ O”时,我想if board[marker_pos] == "X" or board[marker_pos] == "O":抓住它并重新启动功能,所以用户再次看到输入字段。

It does that, but as you can see below in the two pictures, when I try to put the mark in an already occupied field, the function seems to execute completely. 这样做是可以的,但是正如您在下面的两张图片中所见,当我尝试将标记放在一个已经占用的字段中时,该功能似乎已完全执行。 I thought it would get interrupted when I call the function from within itself (player_input(board) ). 我以为当我从自身(player_input(board) )内部调用函数时,它会被中断。 So it shows me the result of display_board(board) and print "iTurn is %s " %iTurn twice or however many tries it took me to put the mark in an empty position. 因此它向我显示了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)

edit: This would be the code for the while loop 编辑:这将是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

I think the problem is, that you don't exit the function which got an invalid position. 我认为问题是,您不退出位置无效的函数。 That's why - after the player_input with the valid position is run - the one called before also runs the last commands. 这就是为什么-在运行具有有效位置的player_input之后-之前调用的那个也会运行最后一个命令。 This can be fixed if you simply return after calling player_input again. 如果您只是在再次调用player_input之后返回就可以解决此问题。

Change the player_input(board) s to player_input(board)更改为

return player_input(board)

or 要么

player_input(board)
return

You can use while True to repeat it and break to exit loop 您可以使用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)

You can write it little simpler 您可以写得更简单

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