Dosent work


It looks like not all your rows have 5 columns

Fix the error

for i in range(5):
print board


so you are appending list to list since your ls is list too, if you want it to be done this way you need to do
ls = ["0"]*5 and put it in for loop


that dosent work since it says board is not a list and it takes only the single row of OOOOO in the board



your code is almost right! ls = ["0"] * 5. and stays where it is

ls= ["O"] * 5
for i in range(5):
print board


Shows this error on doing that:
It looks like your board has 5 references to the same row! This will cause problems later on. Read the hint for more information and use a for loop to complete this lesson


copy your code, what I said in my first post is solution for your problem, I tested it and it works

you need to put ls variable inside for loop in order for it to work


@knezknele it doesn't matter if ls in inside or outside the loop

@yash1598 after trying a little harder i discovered a solution

the code solutions that we have discussed here will run in a python interpreter.

the issue with our code in this instance is that codecademy wants you to write it in a specific way, as follows

board = []

for i in range(5):
    board.append(["O"] * 5)


run the following in an interpreter for an example of why it is written as above

board = []
ls = ["O"] * 5
for i in range(5):

print board
board[2][0] = 'X'
print board

the for loop adds 5 references to ls


Tossing in a list comprehension, as a refactored solution

board = [ [ 'O' ] * 5 for i in range(5) ]

Because ls is a list, it is not a value that can be copied directly. What is happening here is the array is filling up with five identical references, all to the same object. They are not five unique rows, but five rows all with the same references.

board = [ [ls], [ls], [ls], [ls], [ls] ]

The workaround would be to append a slice in each row...


Follow the above advice and appending each reference to the board inside the loop looks like this:

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


inside and outside, output is the same, but no error in second :stuck_out_tongue: