Reason behind the formation of the output


#1

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

THANK YOU


#2

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


#3

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 ?
Url:
Make a list


#4

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


#5

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


#6

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']

#7

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