BugLab017 / 020up to 100
listsreferencesmutationgotchahard

The Shared Row

The function create_board(size) creates a square game board filled with zeros and places a 1 in the center cell. Only the center cell should be 1 — all other cells should be 0.

Instead, every row in the board gets the 1, not just the middle one. Changing one cell somehow changes all of them.

The board-creation line looks completely normal. What is * size actually doing to the rows?

# your fix
+ must use for
hint 2after hint 1
hint 3after hint 2
solution

Read the code. Before running anything: what does the last line below give?

def create_board(size):
"""
Creates a square game board of the given size, filled with zeros,
then places a 1 in the center cell. Returns the board.
Only the center cell should be 1 — all other cells should be 0.
Example: create_board(3) -> [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
"""
board = [[0] * size] * size
mid = size // 2
board[mid][mid] = 1
return board
>>> create_board(3)