0
python, help
import random def matrix(rows): mat = [[random.randint(0,10) for j in range(rows)] for i in range(rows)] for i in range(rows): print (mat[i]) def tr_mat (mat): rows = len(mat) cols = len(mat[0]) tr_mat = matrix(cols, rows) for i in range(rows): for j in range(cols): tr_mat[j][i] = mat[i][j] return tr_mat mat=matrix(3) print(tr_mat(mat)) help me to change the code, error is the (object of type 'NoneType' has no len()) and it about rows and cols in (def tr_mat)
1 Odpowiedź
+ 3
Here are a few points to notice:
● Your matrix function is not returning anything, therefore:
mat=matrix(3)
Sets the <mat> to None, there was nothing returned by matrix function. The error you get was triggered when you try "rows = len(mat)", but <mat> is None. Add "return mat" after the for loop to show the matrix.
● In the tr_mat function you try to call the matrix function passing two arguments, <cols> & <rows>;
tr_mat = matrix(cols, rows)
But the matrix function signature shows the matrix function only accepts one parameter, either change the matrix function signature, or how it is called in tr_matrix function.
Hth, cmiiw