In below Python program python program for matrix addition of 2*2 giving same output (Wrong) for both rows when matrix assigned zero matrix3=[[0]*col]*row
and assigned values to matrix. Give correct output for append operation.
row = int(input("Enter the number of rows:"))
col = int(input("Enter the number of columns:"))
matrix1 = []
for i in range(row):
a1 =[]
for j in range(col):
a1.append(int(input()))
matrix1.append(a1)
print("1st matrix")
for i in range(row):
for j in range(col):
print(matrix1[i][j], end = " ")
print()
print("Enter Elements of 2nd Matrix")
matrix2=[]
for i in range(row):
a2 =[]
for j in range(col):
a2.append(int(input()))
matrix2.append(a2)
print("2nd matrix")
for i in range(row):
for j in range(col):
print(matrix2[i][j], end = " ")
print()
matrix3=[[0]*col]*row
print("additing matrices:")
'''
#Give Correct output
for i in range(row):
matrix3.append([])
for j in range(col):
matrix3[i].append(matrix1[i][j]+matrix2[i][j])
'''
#Give Wrong Output for 2*2 matrix with both rows as same
for i in range(row):
for j in range(col):
matrix3[i][j]=matrix1[i][j]+matrix2[i][j]
for i in range(row):
for j in range(col):
print(matrix3[i][j], end = " ")
print()
Output For Code