简体   繁体   中英

I don t understand this for loop

def available_moves(self):
    moves = []
    for (i, spot) in enumerate(self.board):
        if spot == ' ':
            moves.append(i)
    return moves

I am making a python tic tac toe project from a tutorial but I don t understand the for loop I don t get what for (i, spot) means

The following code:

for (i, spot) in enumerate(self.board):

loops through the items in board and given you each items index along with the item.

Specifically, i here refers to the position in the collection of the current element, with spot being the current element from the board collection.

If you do this code you Know what he is each (i, spot) !!

x = enumerate(self.board)

print(list(x))

i = 0, 1, 2... spot = '???'..

(i, spot) = [(0, '???'), (1, '???'), (2, '???')]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM