Make a list (BattleShip)


#1



Make a list in the Battleship Excercise


Getting "Oops, try again. It looks like the rows are not represented as lists." Even when the output IMO looks to be perfect


I got the result

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

Which looks just like it should, no?


board = []

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

^^ That's my code and I am very confused why I'm getting an error when the output looks perfect.


#2

The board should get the * 5 when you append the row. That will give 5 rows of 5 independent cells.

Recall that each row is a list, so to print without the list syntax, use ' '.join(row) to print each row.

Not exactly. it looks perfect but it is incorrect, per the above noted.


#3

OK. I'm not fully understanding what you're talking about with the .join(row) mechanic, maybe that's what I'm missing now. I have the code giving me the correct answer but It's all part of the same row. Separate rows weren't created.

my code:

board = []

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

and the output is:

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


#4

That looks correct. You may have to advance to the next lesson to cover list.join().


#5

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