How can I append a list of “O”s to the board?

Question

How can I append a list of “O”s to the board?

Answer

If you’re getting an error message that says something along the lines of Oops, try again! It looks like the rows are not represented as lists., it’s likely because you’ve tried to .append() the character "O" by itself, not as a list.
In the example we see that print ["O"] * 5 results in a single list containing 5 "O"s. This is exactly what we want to be .append()ing to our board inside of the for loop.
If we forget to put brackets around the "O", it ends up just adding 5 strings with 5 "O"s each, like this:
['OOOOO', 'OOOOO', 'OOOOO', 'OOOOO', 'OOOOO']

8 Likes

for O in range(0,5) :
board.append( “O” * 5)

1 Like

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

5 Likes

it is supposed to speech marks

This exercise was confusing, in part because it isn’t clearly stated what form the result is expected to take.

The exercise is intended to build a list with five items in it. Each of those items is another, nested list, where the letter O is repeated five times per sub-list:

[[‘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’]]

9 Likes

5 posts were split to a new topic: Tired of using the solutions to get unstuck. nice knowing you all

So, here’s what I have:

board = []
for printingboard in range(5):
  board.append(["OOOOO"])
  print board

And ends up with this:
[[‘OOOOO’]]
[[‘OOOOO’], [‘OOOOO’]]
[[‘OOOOO’], [‘OOOOO’], [‘OOOOO’]]
[[‘OOOOO’], [‘OOOOO’], [‘OOOOO’], [‘OOOOO’]]
[[‘OOOOO’], [‘OOOOO’], [‘OOOOO’], [‘OOOOO’], [‘OOOOO’]]
And says that not every row has 5 columns. ???

Hello @dev8436547269!

It may be that the exercise is looking for the following output.

[["OOOOO"], ["     "], ["     "], ["     "], ["     "]]
[["OOOOO"], ["OOOOO"], ["     "], ["     "], ["     "]]
[["OOOOO"], ["OOOOO"], ["OOOOO"], ["     "], ["     "]]
[["OOOOO"], ["OOOOO"], ["OOOOO"], ["OOOOO"], ["     "]]
[["OOOOO"], ["OOOOO"], ["OOOOO"], ["OOOOO"], ["OOOOO"]]

I’ve tried many other ways but show either
[[“OOOOO”], [" “], [” “], [” “], [” “]]
[[“OOOOO”], [“OOOOO”], [” “], [” “], [” “]]
[[“OOOOO”], [“OOOOO”], [“OOOOO”], [” “], [” “]]
[[“OOOOO”], [“OOOOO”], [“OOOOO”], [“OOOOO”], [” "]]
[[“OOOOO”], [“OOOOO”], [“OOOOO”], [“OOOOO”], [“OOOOO”]]
or
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO

FURTHERMORE, I have tried many possible ways.

Could you provide a link to the exercise so I can see what the instructions are asking for?

@dr_victoria https://www.codecademy.com/courses/learn-python/lessons/battleship/exercises/make-a-list

The expected final output if we just print(board) will 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']]

We will be creating a print_board function that will output only the contents of the cells, no quotes, no brackets, and it will be in five rows.

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
1 Like

@mtf How is this done?

We need all the cells to be independent so we generate them individually.

# create an empty list

board = []
# iterate over five rows...

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

Next we want to test that each element is distinct.

board[0][0] = 'X'

board[4][4] = 'X'

Let’s make a function to print the board…

def print_board(board):
    for i in range(len(board)):
        print (board[i])

print_board(board)
['X', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'X']

We can refine the print_board() function to suit, but for now we have verified that each cell is independent of the others.

5 Likes