Dosent work


#1



https://www.codecademy.com/courses/python-beginner-en-4XuFm/0/3?curriculum_id=4f89dab3d788890003000096#

It looks like not all your rows have 5 columns

Fix the error

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


#2

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


#3

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


#4

@yash1598

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

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

#5

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


#6

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


#7

@chris.rose.one
you need to put ls variable inside for loop in order for it to work


#8

@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)

#9

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):
    board.append(ls)

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

the for loop adds 5 references to ls

#10

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...

        board.append(ls[:])

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

#11

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