nischalinn

February 20th, 2012, 05:59 AM

I want to explain my code to you.

1. this code is to search for the given number whether it is present in the list or not.

2. first if statement returns the value of row if the i/p number is in the first column

3. if the given number is not in the first column, the second if statement compares the i/p number with the values in the first column of different rows.

4. if the i/p number is less than the value of matrixM[row+1][0], it goes to

matrixM[row][0] i.e. previous row to search the i/p number and return index.

Suppose the i/p number is 22, since it is not in the first column, the second if statement is executed.

when row = 2, row+1 = 3 =>matrixM[3][0] == 28, since 22<28, it should go to the third row and search for 22 & return the value of row and column.

Why this code is showing error in main()?

def checkvalue(number,matrixM):

for row in range(len(matrixM)):

if (matrixM[row][0] == number):

return row,0

elif(matrixM[row+1][0] < number):

for column in range(len(matrixM)):

if(matrixM[row][column] == number):

return row, column

def main():

matrix = [[2,4,6,8],[10,12,14,16],[20,22,24,26],[28,30,32,34]]

row1=0

column1=0

n = int(input(('provide the value of n: ')))

row1,column1 = checkvalue(n,matrix)

print('\n')

print(row1,column1)

return 0

if __name__ == '__main__':

main()

provide the value of n: 22

Traceback (most recent call last):

File "check.py", line 48, in <module>

main()

File "check.py", line 42, in main

row1,column1 = checkvalue(n,matrix)

File "check.py", line 30, in checkvalue

elif(matrixM[row+1][0] < number):

IndexError: list index out of range

1. this code is to search for the given number whether it is present in the list or not.

2. first if statement returns the value of row if the i/p number is in the first column

3. if the given number is not in the first column, the second if statement compares the i/p number with the values in the first column of different rows.

4. if the i/p number is less than the value of matrixM[row+1][0], it goes to

matrixM[row][0] i.e. previous row to search the i/p number and return index.

Suppose the i/p number is 22, since it is not in the first column, the second if statement is executed.

when row = 2, row+1 = 3 =>matrixM[3][0] == 28, since 22<28, it should go to the third row and search for 22 & return the value of row and column.

Why this code is showing error in main()?

def checkvalue(number,matrixM):

for row in range(len(matrixM)):

if (matrixM[row][0] == number):

return row,0

elif(matrixM[row+1][0] < number):

for column in range(len(matrixM)):

if(matrixM[row][column] == number):

return row, column

def main():

matrix = [[2,4,6,8],[10,12,14,16],[20,22,24,26],[28,30,32,34]]

row1=0

column1=0

n = int(input(('provide the value of n: ')))

row1,column1 = checkvalue(n,matrix)

print('\n')

print(row1,column1)

return 0

if __name__ == '__main__':

main()

provide the value of n: 22

Traceback (most recent call last):

File "check.py", line 48, in <module>

main()

File "check.py", line 42, in main

row1,column1 = checkvalue(n,matrix)

File "check.py", line 30, in checkvalue

elif(matrixM[row+1][0] < number):

IndexError: list index out of range