Make a list help!


board = []
for x in range(0,5):
print board

error: Oops, try again. It looks like your board contains characters other than "O".

I don't know what's wrong, please help.


Hi this line


you wrote o instead of O should be uppercase


thank you so much, that worked


I got it right, according to Codecademy, but when I print it out, I don't get a neat 5X5 matrix, I get a mess:
[['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'], ['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']]
[['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']]

My code:
board = []
for i in range(5):
board.append(['O'] * 5)


Interesting, I get the same. I'll be keen to see if someone has some insights. It says success, but I have 15 x 5 matrix, like yours...


You printed Board instead of just printing ["O"]*5. That's why you got that messed up output


If I may have quick question with respect to the same question, what is the difference between?

for x in range(0,(len(board))): and for x in range(0,5):

I thought that len(board) is 5 and thus it should work as well but it doesn't.


len(board) WILL be 5 when you append 5 items (that is in the end of your code).
But at first (when the loop is supposed to be executed for the first time) board is empty.


"Print" is inside the loop (blank space from the left side is as wide as loop containing).
So it is execuded with every iteration of the loop:
1st loop execution - when board has ["O"]*5;
2nd loop execution - when board has ["O"] x5, ["O"]x5;
and so on.



for i in range(0,5):
print board


Hi maria_pl, thank you for the reply. Just one more question if I may.

I thought that len(board) returns 5 as it as 5characters (1 is b, 2 is o,...) but I guess this is incorrect from what you have said. So does len() depend what is in the list?


I haven't notice that word "board" has 5 characters)))
But if you want to use this accident don't forget the quotes: len("board")
Otherwise it counts what is inside the list with the same name.


Aha, got it. I see what caused me the confusion now. Thank you! Really appreciate it! :slight_smile:


Not at all! :slight_smile:


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