Random row and column in the grid 5*5


#1



def random_row(board):
return randint(0, len(board) - 1)

def random_col(board):
return randint(0, len(board[0]) - 1)


Hi,
Can someone help me explain the difference between randint(0, len(board) - 1) and randint(0, len(board[0]) - 1)? why is the latter used for random column?

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

Thanks a lot!
Hoa


#2

we have a multidimensional list for our board (an inner and outer list), the outer list contains the rows, and the inner lists contains the columns. By accessing any random row (board[0]) we get access to the columns.

If your board wouldn't be square, not using this technic means either: your ship can't be positioned everywhere, or you might even get an index error


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.