Answer the question
In order to leave comments, you need to log in
Throws an IndexError: list index out of range error. What's the matter?
matrix = []
import random
m,n = int(input()),int(input())
for i in range(m):
for j in range(n):
print(i,j)
matrix[i][ j] = random.randint(1,9)
print()
print(matrix)
Answer the question
In order to leave comments, you need to log in
matrix - you have an empty list, and you are trying to write something into the [i, j]-th cell. The error about that also says that there is an address on an invalid index.
You, after entering m, n, do this:
And only then do your 2 cycles with generation.
Well, or in cycles, add new elements to matrix:
matrix = [[0 for x in range(n)] for y in range(m)]
matrix = []
for i in range(m):
matrix.append([])
for j in range(n):
matrix[i].append(random.randint(1,9))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question