Write a python program and add a function called add_matrix() to it. This function takes two 2-D lists of integers (matrix) as parameters and computes their sum, if a sum can actually be computed. Otherwise, it returns an empty list. Note that, two matrices can only be added together if they have the exact same dimensions. If the dimensions are acceptable, then the resulting matrix will also have the same dimensions as its operands, and the value of each element in the resulting matrix is the sum of the corresponding elements in the input matrices. Hint: test rectangularity (the number of rows and the number of elements in each row must be the same in two matrices) before attempting addition. Sample output: >>> matrix1 = [[2,3], [4,5]] >>> matrix2 = [[9,0], [8,6]] >>> print(add_matrix(matrix1, matrix2)) [[11, 3], [12, 11]]
Write a python program and add a function called add_matrix() to it. This function
takes two 2-D lists of integers (matrix) as parameters and computes their sum, if a sum
can actually be computed. Otherwise, it returns an empty list. Note that, two matrices can
only be added together if they have the exact same dimensions. If the dimensions are
acceptable, then the resulting matrix will also have the same dimensions as its operands,
and the value of each element in the resulting matrix is the sum of the corresponding
elements in the input matrices.
Hint: test rectangularity (the number of rows and the number of elements in each row
must be the same in two matrices) before attempting addition.
Sample output:
>>> matrix1 = [[2,3], [4,5]]
>>> matrix2 = [[9,0], [8,6]]
>>> print(add_matrix(matrix1, matrix2))
[[11, 3], [12, 11]]

Step by step
Solved in 3 steps with 1 images









