Help with battleship


#1

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', 'O']]

My code:

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

#2

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


#3

sorry wrote the code wrong my code is

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

#4

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.


#5

I got it thank u very much


#6

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


#7

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