Reason behind the formation of the output


I was doing the project of the battlefield and have on the piece of code.

for x in range(0, 5):

Can I get an explanation of the above code and why is this required



Please post the full code. And a link to the exercise would also come in handy


board = []

for x in range(0, 5):
board.append([“O”] * 5)

What does the codefor x in range(0,5): means and what’s the reason behind using this code ?
Make a list


we need to make a 5x5 board, board.append([“O”] * 5) appends a row, but we need to 5 rows, so we use a for loop to do so


Instead of typing board.append([‘O’] * 5), can I write Board = [‘O’] *5
or Board = [‘O’,‘O’,‘O’,‘O’,‘O’]?
Thank you


no, that will generate a 1x5 board, not a 5x5 board like with the loop, you can see the 5x5 board if you want:

board = []

for x in range(0, 5):
    board.append(["O"] * 5)

print board

which is very different from:

['O', 'O', 'O', 'O', 'O']


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.