+ 1
What is the mistake in this code
4 Answers
+ 3
#read the comments to know about errors ; hope it helps to correct code by yourself..
def transpose(m1):
m2=m1
# here missing outer loop for j
for i in range(0,len(m1)):
m2[i][j]=m1[i][j] # think about logic again, you just return same array..
return m2
row=int(input("Enter the number of rows"))
column=int(input("Enter the number of columns") # ) missing
M1=[]
for i in range(0,row):
r=list(input("Enter the row to be added") # ) mising
M1.append(r)
M2=transpose(M) #M1, not M
for j in range(0,row):
for i in range of (0,column): # what is range of ? just use range
print(M2[j][i],end=" ")
print()
+ 2
def transpose(m1,c,r):
m2=[]
for i in range(0,c):
a=[]
for j in range(0,r):
a.append(j)
m2.append(a)
for j in range(0,c):
for i in range(0,r):
m2[i][j]=m1[j][i]
return m2
+ 1
https://code.sololearn.com/cI60QjxlwiB7/?ref=app
Now what is the mistake ?
+ 1
You are actually declaring 1d list but trying to access 2d list.
In addition to @Oma Falk,
While taking input also, first add the current row to a empty list. then add total list of row values to another list, that makes a 2d list..
hope it helps.