$ python test.py
Enter the limit of your matrix:3
Enter the element:1
Enter the element:2
Enter the element:3
Enter the element:1
Enter the element:2
Enter the element:3
Enter the element:1
Traceback (most recent call last):
File "test.py", line 209, in <module>
a[i][j]=int(input("Enter the element:"))
IndexError: list index out of range
Your code work for 2*2 matrix, because you crested only two rows.
a=[[random.random()for i in range(n)],[random.random()for j in range(n)]]
Following is output of a for 3*3 matrix:-
[[0.9632434262652646, 0.7470504655963679, 0.2750109619917276], [0.7032133906246875, 0.16200573351318048, 0.09565026547688305]]
overcome to above issue see following code:
Algo/steps:
- Get Matrix limit n*n
- Create matrix of n*n
- By lambda, map and sum function get sum of all rows and column.
- No need of random method.
code:
n = int(raw_input("Enter the limit of your matrix:"))
a = []
tmp = [0 for k in range(n)]
for i in range(0,n):
a.append(list(tmp))
for j in range(0,n):
a[i][j] = int(input("Enter the element:"))
sumr = sum(map(lambda x:sum(x), a))
sumc = 0
for j in range(0,n):
sumc +=sum(map(lambda x:x[j], a))
print sumr
print sumc
Output:
Enter the limit of your matrix:3
Enter the element:1
Enter the element:2
Enter the element:3
Enter the element:1
Enter the element:2
Enter the element:3
Enter the element:1
Enter the element:2
Enter the element:3
18
18