Help with battleship


I know we can use two for loops to print a matrix but I dont understand how to do what
code academy is asking in make a list

Plz help me to make the code
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']]

My code:

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


how did you print this? If you just do:

print board

you get:

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

also, you will make a nice print in one of the next exercises


sorry wrote the code wrong my code is

for i in range(0,len(x)):
print board


still produces the right 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 might look a bit messy because of the small output window. I do hope you did indent the board.append(x) properly.


I got it thank u very much


you will make a function to make a nice representation of the board in one of the next exercises


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