# Board[0]

#1

In the lesson no.7, we were taught to write this code below.

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

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

Why is it now in lesson 8, it changed into below? What does it change, what's the difference and what is the purpose of adding board[0]? and why the row is still unchanged?

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

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

#2

this will generate a random number for the rows:

``randint(0, len(board) - 1)``

and this for the columns:

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

using `board[0]` will allow you to make a board with different numbers rows then cols

#3

so not using board[0] will prevent the same number rows and cols?

#4

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

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

so regardless I put board[0] in row or col, is also the same right?

#5

yea, now it doesn't matter because your board is 5x5. But what if you have a 10 x 8 board?

#6

``````board = []

for x in range(10):
board.append(["O"] * 10)

def print_board(board):
for row in board:
print " ".join(row)

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

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

is this correct or should I put the board[0] to the random_row function?
also, if I want to make a 10x8x8 board, what will board[0] change into?

#7

well first make a 10 by 8 board:

``````for x in range(10):
board.append(["O"] * 8)``````

now you can't do this:

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

given this might generate an invalid coordinate.

#8

``````for x in range(10):
board.append(["O"] * 8)

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

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

is this correct?
is it invalid because board[0] means the board that is displayed already which is (["O"] * 8)?

#9

Why -3? We now calculate the length of the column, so no need to do minus 3

this:

``````for x in range(10):
board.append(["O"] * 8)``````

will generate 10 rows, and 8 columns. so you can do:

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

you need to minus one to compensate for the fact that lists are zero indexed based, if you where to do:

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

you might also get index 8 and 9 for your columns, which are invalid coordinates.

#10

Ah! I get it now. Board[0] is the list [] index column which is 8.
randint(0, len(board[0]-1))

And board is the range, the index row which is 10.
randint(0,len(board-1))

And every index -1 because index zero.
Am i right?

#11

you have your board, which is a multi-diminsional list. len(board) will give you the number of items in board (which is your number of rows)

len(board[0]) will get the item at index 0, which is the list containing your columns.

yes, you are right

#12

Okay now I get it! Thank you stetim is it possible for me to make a 10x9x8 dimension?

#13

a 3d board?

possible? Maybe
recommend: Not at the moment

#14

I'll be patience until then, if you found anything interesting please DM me Thank you again!

#15

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