7. Hide


#1

Hello,
The code is correct. I just don't understand why/how "randint(0, len(board) - 1)" works.
Please can anyone explain?

Thank you

from random import randint 

board = []

for x in range(0, 5):
    board.append(["O"] * 5)

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

# Add your code below!
def random_row(board):
    return randint(0, len(board) - 1)
def random_col(board):
    return randint(0, len(board) - 1)
random_row(board)
random_col(board)

#2

randint() generates a RANDom INTeger. We want ship coordinates, so it needs be a row on the board (0, 1, 2, 3 or 4)

If we now where to change the board dimensions, the random integers automatically generate the right value, if we where to hard code the numbers, this wouldn't happen, so its considered bad practice, you shouldn't

so instead we use the length of board to make our code more flexible to change


#3

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