in function entery when the first time argument is going (n, a) it's working fine a goes as empty list initialized with 0. but in the second time when b matrix is going as argument (n, b), so at that time li list which was returned by the function before is going as matrix b rather than empty matrix b which was initialized by 0. someone please help me finding error in my code.
#funtion for taking input in matrix
def entery(n, li):
print(li)
print('enter the element of matrix: ')
for a in range(n):
for b in range(n):
no = int(input())
li[a][b]= no
return li #returning the updated matrix
n = int(input('dimensions of square matrix:'))
a = []
c = []
result = []
#creating matrix
for i in range(n):
b =[]
for j in range(n):
b.append(0)
a.append(b)
c.append(b)
result.append(b)
print("a matrix",a)
print("c matrix",c)
fm = []
sm = []
#calling function
fm = entery(n,a)
sm = entery(n,c)
print(fm)
print(sm)
#addition of matrix
for x in range(n):
for y in range(n):
result[x][y] = fm[x][y] + sm[x][y]
print(result)
the output is shown below-
output:
dimensions of square matrix:2
a matrix [[0, 0], [0, 0]]
c matrix [[0, 0], [0, 0]]
[[0, 0], [0, 0]] #when called first time
enter the element of matrix:
11
22
33
44
[[11, 22], [33, 44]] # but in second time...
enter the element of matrix:
12
23
34
45
[[12, 23], [34, 45]]
[[12, 23], [34, 45]]
[[24, 46], [68, 90]]