Make a list help!


#1

board = []
for x in range(0,5):
board.append(["o"]*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.


#2

Hi this line

board.append(["o"]*5)

you wrote o instead of O should be uppercase


#3

thank you so much, that worked


#4

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)


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


#6

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


#7

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.


#8

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.


#9

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


#10

board=[]

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

#11

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?


#12

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.


#13

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


#14

Not at all! :slight_smile:


#15

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