Explain this code


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-python/lessons/battleship/exercises/custom-print?action=lesson_resume

<In what way does your code behave incorrectly? Include ALL error messages.>
it works

<What do you expect to happen instead?>
i just someone to explain why it works

```python

Replace this line with your code.
board = []
for i in range(5):
board.append([‘O’] * 5)
def print_board(board_in) :
for row in board:
print row
return row

<do not remove the three backticks above>

#2

An empty list stored as board.

range(5) is an iterable, [0, 1, 2, 3, 4]. The for loop will iterate 5 times.

['O'] * 5 is a list of O’s => [‘O’, ‘O’, ‘O’, ‘O’, ‘O’]

board.append() adds a new item to the list, board.

Should be using the parameter variable, but assuming it still works means that the function is reaching to outer scope for the global, board.

for row in board_in:

The above iterates over the list and looks up one row at a time.

The function is printing, not returning. That line is not needed.


#3

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