I'm trying to create a function that adds two 3x3 matrices together, but my output doesn't give me the desired values.
I added the print-function in the nested for-loops to see how the code was working:
sum_of_a_b = [[0] * 3] * 3
for i in range(0, 3):
for j in range(0, 3):
sum_of_a_b[i][j] = matrix_a[i][j] + matrix_b[i][j]
print(answer)
Output:
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]
[[1, 9, 0], [1, 9, 0], [1, 9, 0]]
[[1, 9, 5], [1, 9, 5], [1, 9, 5]]
[[1, 9, 5], [1, 9, 5], [1, 9, 5]]
[[1, -2, 5], [1, -2, 5], [1, -2, 5]]
[[1, -2, 5], [1, -2, 5], [1, -2, 5]]
[[6, -2, 5], [6, -2, 5], [6, -2, 5]]
[[6, 7, 5], [6, 7, 5], [6, 7, 5]]
[[6, 7, 8], [6, 7, 8], [6, 7, 8]]
[[6, 7, 8], [6, 7, 8], [6, 7, 8]]
Why does my code return the sum of a+b(i,j) on every list rather than the specific element in each list?