I'm attempting to increment the values in a 2d array for a game I'm making, but I'm getting the same value in each array instead. This is the code:
def matrix(grid):
nrows = len(grid)
ncols = len(grid[0])
for i in range(nrows):
for j in range(ncols):
grid[i][j] += 10
for row in grid:
print(row)
rows = 4
cols = 4
grid = [[0 for i in range(cols)] for i in range(rows)]
matrix(grid)
The output is:
[10, 10, 10, 10]
[10, 10, 10, 10]
[10, 10, 10, 10]
[10, 10, 10, 10]
Where as I would like it to be
[10, 20, 30, 40]
[10, 20, 30, 40]
[10, 20, 30, 40]
[10, 20, 30, 40]
Also, is it possible to stagger and use two nested for loops to provide incremented values for each row? Such as:
[10, 20, 30, 40]
[20, 40, 60, 80]
[10, 20, 30, 40]
[20, 40, 60, 80]