0

Code is for accepting numbers from user and creating a matrix using list then adding two matrices.i am not getting the output

can someone rectify my mistake in this code . I want to accept 2 matrices using list and then add them. r,c=3,2 x=[] for i in range(r): x.append([0]*c) for i1 in range(r): for i2 in range(c): x[i1][i2]=int(input()) print() y=[] for m in range(r): y.append([0]*c) for m1 in range(r): for m2 in range(c): y[m1][m2]=int(input()) print() z=[[0,0,0],[0,0,0]] for j in range(len(x)): for k in range(len(y)): z[j][k]=x[j][k]+y[j][k]

7th Nov 2018, 3:45 PM
Mara
3 odpowiedzi
+ 7
My recommendation: x = [] for i in range(c): # outer x.append([]) for j in range(r): # inner x[i].append(int(input())) # x = [[a00, a01, a02], # [a10, a11, a12]] much clearer, although it may be slower because of append
7th Nov 2018, 4:10 PM
Flandre Scarlet
Flandre Scarlet - avatar
+ 3
You used 2 rows and 3 columns for z. But it should look like z=[[0, 0], [0, 0], [0, 0]] Also, the line before the last one should have for k in range(len(x[0]): That's because for the inner loop, we're iterating over the columns. We could also use range(c).
7th Nov 2018, 4:02 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
To get an output you must say what you want to print f.e. print("HELLO") print(x)
11th Nov 2018, 6:43 AM
Mirko Klotzsche
Mirko Klotzsche - avatar