Make a List


#1



Oops, try again. It looks like the rows are not represented as lists.

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


This is what my board looks like

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

so im very confused as to what im doing wrong? do i maybe have to concatenate them
 to get rid of the comma? lol maybe ive been at my comp for too long. any help/explanation
 would be greatly received

my code


#2

You're currently printing board five times, print it once and then compare to what it's supposed to be (which is five rows of five columns)


#4

ahh okay so i need to to change the board = ["O"] * 4 line, or maybe add another line to overwrite the the board line. Thanks jonaton your answers are very much appreciated, i always seem to get stuck on the easy stuff. How long have you been coding?


#5

okay so i commented out the line board = ["O"] * 4 and it works and the lesson passed but the result 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'], ['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']]

thats not a 5 * 5 grid! im really confused now, can anyone shed some light on this situation


#6

Why do you use the number 4 when the size is 5 in both dimensions?

The last value is a 5x5 list, why are you printing four other values as well?


#7

for some reason it was printing 6 sets of lists so i took one away. ive pushed through to lesson six and now it wants me to put it in a 5*5 grid, it seems i was getting ahead of myself.


#8

As I have a similar problem I attach my question and my not-correct-working-code

board = []
for ocean in range(0,5): #creates 5 empty lists one below the other
    board = (['O']*5) #fill the lists with 5 "O" each
    print board

OUTPUT:

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

It looks pretty simple but I tried it for quiet a long time (incl. many div. codes with .append())- how can I create instead of 5 separate list one list containing the 5 ones?
I assume the right answer should be:

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

Many thanks!


#9

You'd put five lists in a list. How did you put the O's in the list? You'd do the same thing to put lists in a list


#10

Ok - no idea, I tried it now for hours and have a huge amount of "nearly-correct-working-code" but I no

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

:sweat:

other question - is it important that each list of the list is in a separate line?

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

works (and I can go on with the next lesson) but the lists are not separate?!?


#11

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