0

I want to input a matrix in the following form:

1 2 3
4 5 6
7 8 9

I have tried this code

m1=[]
r,c=input().split(" ")
print(r,"and",c)
for x in range (0,r):
    for y in range (0,c):
        m1[x][y]=input().split(" ")
    print("\n")    
print (m1)

but get an exception instead:

IndexError: list index out of range
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    Well, your `m1` list is empty. You can't assign to indices that are not there. You need to use `.append()` or `.extend()` or a list comprehension to add indices. – Martijn Pieters Jun 19 '17 at 06:36
  • m1=[] r,c=input().split(" ") print(r,"and",c) for x in range (0,int(r)): for y in range (0,int(c)): m1.append(input().split(" ")) print("\n") print (m1) its taking continuously inputs not printing matrix – Anuradha.ch Jun 19 '17 at 06:54

0 Answers0