Hide and seek! Help please


#1

Instructions
1. Define two new functions, random_row and random_col, that each take board as input.
2. These functions should return a random row index and a random column index from your board, respectively. Use randint(0, len(board) - 1).
3.Call each function on board.

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

This is from the Hide... excercise and this code get executed but in the later exercise ..and Seek! The code automatically changed to :

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

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

What is the difference? and what does len(board[0] -1) give?


#2

Hi, @daanishdurrani,

len(board[0]) gives the number of columns in the first row (row 0).

Therefore, len(board[0]) - 1) gives the index of the final column in the row. Since all rows have the same number of columns, it applies to every row.

The change was made because although the Battleship! board is a square, this would also work for a rectangular board that is not a square.


#3

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