打破多个循环python - 具体问题

发布于 2024-11-08 11:04:01 字数 2413 浏览 0 评论 0原文

我的问题是关于第二个中断语句: 如果 x == xstart 且 y == ystart: 那么

这个break语句跳出了while true循环,对吗?但是然后它会返回到下一个最近的循环(即 while board[x][y] = OtherTile:) 但因为不满足这个条件,所以它会返回到原来的 for xdirection, ydirection 循环?我只是想看看我的理解是否正确。

if board[xstart][ystart] not OnBoard(xstart, ystart) or board[xstart][ystart] != ' ':
    return False

#temporarily set the tile on the board, but change it back to a blank before the end of this function
board[xstart][ystart] = tile

#Set computer tile
if tile == 'X':
    OtherTile = 'O'
else:
    OtherTile = 'X'

#empty list of tiles to flip
TilesToFlip = []

#this is a for loop for two variables.  Each list of two represents a position away from the orignal spot
for xdirection, ydirection in [[1,0], [0,1], [-1,0], [0,-1], [1,-1], [1,1], [-1,1], [-1,-1]]:

    #we set x and y to the original coordinates passed to us because we want to preserve the orignal values
    x, y = xstart, ystart
    x = x + xdirection
    y = y + ydirection

    #after the first iteration, check to see if the adjacent piece is on the board and if it's the OtherTile:
    if isOnBoard(x,y) and board[x][y] == OtherTile:
        x = x + xdirection
        y = y + ydirection
        #if the next piece is not on the board, go back to the for loop to test another direction
        if not isOnBoard(x,y):
            continue
        while board[x][y] = OtherTile:
            x = x + xdirection
            y = y + ydirection
            #we break here because if we had just continued it would have gone back to the while loop.
            #since we're breaking, it goes back to the original for loop (if it goes off the board)
            if not isOnBoard(x,y):
                break

        #it finishes with the while loop if it reaches a tile that is not the OtherTile (i.e. it's blank or it's the player's)
        #so we check to see if it's the player's
        if board[x][y] == tile:
            #if it is the player's tile, then we go in the reverse direction, appending each tile to the TilesToFlip list
            while True:
                x = x - xdirection
                y = y - ydirection
                #when we reach the original tiles, we break (by then we have all the tiles that need to be flipped in store in the new list)
                if x == xstart and y == ystart:
                    break
                TilesToFlip.append([x,y])

My question is about the second break statement:
if x == xstart and y == ystart:
break

So this break statement breaks out of the while true loop correct? But then does it go back to the next closest loop (namely the while board[x][y] = OtherTile:) But because this condition is not met, it goes back to the original for xdirection, ydirection loop? I just wanted to see if my understanding was correct.

if board[xstart][ystart] not OnBoard(xstart, ystart) or board[xstart][ystart] != ' ':
    return False

#temporarily set the tile on the board, but change it back to a blank before the end of this function
board[xstart][ystart] = tile

#Set computer tile
if tile == 'X':
    OtherTile = 'O'
else:
    OtherTile = 'X'

#empty list of tiles to flip
TilesToFlip = []

#this is a for loop for two variables.  Each list of two represents a position away from the orignal spot
for xdirection, ydirection in [[1,0], [0,1], [-1,0], [0,-1], [1,-1], [1,1], [-1,1], [-1,-1]]:

    #we set x and y to the original coordinates passed to us because we want to preserve the orignal values
    x, y = xstart, ystart
    x = x + xdirection
    y = y + ydirection

    #after the first iteration, check to see if the adjacent piece is on the board and if it's the OtherTile:
    if isOnBoard(x,y) and board[x][y] == OtherTile:
        x = x + xdirection
        y = y + ydirection
        #if the next piece is not on the board, go back to the for loop to test another direction
        if not isOnBoard(x,y):
            continue
        while board[x][y] = OtherTile:
            x = x + xdirection
            y = y + ydirection
            #we break here because if we had just continued it would have gone back to the while loop.
            #since we're breaking, it goes back to the original for loop (if it goes off the board)
            if not isOnBoard(x,y):
                break

        #it finishes with the while loop if it reaches a tile that is not the OtherTile (i.e. it's blank or it's the player's)
        #so we check to see if it's the player's
        if board[x][y] == tile:
            #if it is the player's tile, then we go in the reverse direction, appending each tile to the TilesToFlip list
            while True:
                x = x - xdirection
                y = y - ydirection
                #when we reach the original tiles, we break (by then we have all the tiles that need to be flipped in store in the new list)
                if x == xstart and y == ystart:
                    break
                TilesToFlip.append([x,y])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凝望流年 2024-11-15 11:04:02

它只会打破 while True 循环。循环将永远持续下去,直到 x == xstart 且 y == ystart。

一旦满足该条件,它将继续在外部 for 循环中进行。

It will only break the while True loop. The loop will continue forever until x == xstart and y == ystart.

Once that condition is met, it will continue on in the that outer for loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文