Question: You are given a M∗N matrix, and a variable K, print the resultant matrix after scalar multiplication. Code i have written:
n,m,k=map(int,input().split())
mat1=[]
for i in range(n):
mat1.append(list(map(int,input().split())))
for i in range(n):
for j in range(m):
mat1=mat1[i][j] * k
for i in range(n):
for j in range(m):
print(mat1[i][j],end=" ")
print()
Error:
Traceback (most recent call last):
File "C:/Users/91934/PycharmProjects/pythonProject/main.py", line 8, in <module>
mat1=mat1[i][j] * k
TypeError: 'int' object is not subscriptable
You're replacing the list variable mat1
with a number value with this line, resulting in the said error:
mat1=mat1[i][j] * k
Instead, you should specify the nested list item that is to be assigned by index:
mat1[i][k] = mat1[i][j] * k
or:
mat1[i][j] *= k