And seek


#1


https://www.codecademy.com/courses/python-beginner-en-4XuFm/0/8?curriculum_id=4f89dab3d788890003000096

K so the code is fine, but I'm just wondering about why in this next lesson my random_col function is changed automatically. Does this have to do with having the random_col actually picking a random coloumn while the random_row will pick a row? I'm confused as to how the two randint functions will pick either a row or a column on the battleship grid.


def random_col(board):      #hide... exercise
    return randint(0, len(board) - 1)


def random_col(board):      #...and seek exercise
    return randint(0, len(board[0]) - 1)


#2

Hi @dempancakes,

The Battleship! board is a square, so the number of rows is equal to the number of columns. However, in Exercise 19, Extra Credit, you are invited to ...

... add on to your Battleship! program to make it more complex and fun to play ...

You could decide to modify the board so that it becomes a rectangle in which the number of rows is different from the number of columns. Then the random_row and random_col functions would need to detect the number of rows and number of columns, directly, as appropriate. It is an example of good program design to code the functions that way.

The board object is a list of lists. Specifically, it is list of rows, each of which is a list of cells within that row. The indexes of the rows are numbered from 0 to len(board) - 1, so in the random_row function, we use this to choose a row randomly ...

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

Each of the cells in a row is part of a column. The first row is board[0]. Regardless of the size of the board, we can assume we will have a row 0. The indexes of the columns in that row are numbered from 0 to len(board[0]) - 1, so this is the correct way, in the random_col function, to choose a column within that row, randomly, or within any row, so long as the board is rectangular ...

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

#3

Hey appylpye! Thank you for such a thorough explanation, that was very helpful!


#4