Battleship 3/19: Make a list... Right output but wrong method. Please help


#1



https://www.codecademy.com/courses/python-beginner-en-4XuFm/0/3?curriculum_id=4f89dab3d788890003000096#


Im getting 5 rows of 5 Os but I didn't use a for loop to print 5 times. I am lost. I want the same output but I want to do it the right way.


board = []

for i in range(5):
    board.append("O")
    
print board
print board
print board
print board
print board

Thanks, Ara


#2

Is this the type of thing that they would help me with if I paid the $20/month?


#3

When I do this...

board = []

for i in range(5):
    board.append("O")
for x in board:    
    print board

I get this oops try again message.

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


#4

Your board variable is supposed to a nested list, 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']  ]

as you can see above, there are five sibling lists (each having five 'O's) inside one parent list.

Your code makes board like this:

['O', 'O', 'O', 'O', 'O']

just one list with only five 'O's.

Now that the problem's been sppoted, can you fix it? :wink:


#5

Yup, I appended 5 Os at once.

.append("O", "O", "O", "O", "O")

Thank you for your help


#6

As an alternative, you can use the ['O']*5 syntax to get the result ['O', 'O', 'O', 'O', 'O']. This is what the first section of the instruction is showing. This would make your code more concise, .append(['O'] * 5).


#7

This worked for me:

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

print board

The code is good enough for 4/19 too.


#8

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


#9