10. You win!

#1

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

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

Hi guys. I was meaning to ask, why do we have

`` len(board[0]) - 1``

in the random_col function and don't have it in the row? What does it mean (apart that it subtracts 1 from a number there)? I understand what

``len(board) - 1``

means, but what is this [0] for? Why would they also use random range (0-4) and not (1-5)? Any particular reason? Seems strange to have row 0 and col 0 as a random output. I know it is very subjective, but isn't row/col from 1 to 5 is more "logical" and nicer to an eye? And it's all about adjusting a little the "return" code

``return randint (1, len(board))``

`` return randint(0, len(board[0]) - 1)``

#2

It subtracts 1 from the reported length to accommodate zero-indexing.

``````len(board) == 5

[[0], [1], [2], [3], [4]]  =>  4 == 5 - 1``````

The board[0] focuses on the row length (columns) and takes into account the possibility that the board may not be square. Doing this makes the columns independent of the rows for the purpose of indexing.

#3

``return randint(0, len(board[0]) - 1)``

could you please maybe give another example for this line of code code? What is benefit of using it? What problems can there be if we dont do it their way?

Because when I didn't understand it 1st time, I tried to play with it a little, removed it and it all worked just fine. I returned the code to what Codecademy suggests because I thought it would complicate things otherwise. But I can see now that it is not the case (is it?). If one writes:

``return randint (1, len(board))``

it will work just fine. Unless I'm missing the whole point of making it their way.

Thanks again.

#4

This will generate random numbers in the range `1 .. 5`, and not the required `0 .. 4`

#5

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