Excellent! Now, let’s hide our battleship in a random location on the board.

Since we have a 2-dimensional list, we’ll use two variables to store the ship’s location, ship_row and ship_col.

from random import randint coin = randint(0, 1) dice = randint(1, 6)
  1. In the above example, we first import the randint(low, high) function from the random module.
  2. Then, we generate either zero or one and store it in coin.
  3. Finally, we generate a number from one to six inclusive.

Let’s generate a random_row and random_col from zero to four!



Define two new functions, random_row and random_col, that each take board_in as input.

These functions should return a random row index and a random column index from your board, respectively. Use randint(0, len(board_in) - 1).

Call each function on board.

Folder Icon

Take this course for free

Already have an account?