【问题标题】:break with multiple loops python - specific question打破多个循环python - 具体问题
【发布时间】:2011-05-19 00:06:25
【问题描述】:

我的问题是关于第二个 break 语句: 如果 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])

【问题讨论】:

    标签: python loops break


    【解决方案1】:

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

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

    【讨论】:

    • 是的,但我想知道从 while True 循环中断后它会做什么?它会返回并再次尝试 while board[x][y] = OtherTile: 循环吗?但既然不是真的,那就回到第一个循环(对于 xdirection, ydirection )?
    • 当 while True 循环中断时,它将执行 for 循环的下一次迭代,并从那里继续。如果您希望它再次尝试 while board[x][y] = OtherTile: 循环,则需要缩进从“#it finish with the while loop...”开始的所有内容
    • 一般来说,当一个循环“break-ed”-out-of时,它会在循环结束后的下一条指令处继续。
    猜你喜欢
    • 1970-01-01
    • 2011-11-04
    • 2010-09-16
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    相关资源
    最近更新 更多