Generated random row & col are not the same as list element shown as "X"


I don't have any error in my code. I fulfilled the exercise exactly as said in the instruction but in this game we print out ship_row and ship_col to debug our programming. on the other hand, we specify the list element [guss_row][guess_col] to "X", but when I run my code, the random ship_row and ship_col are not the same as list element that is specified with "X"!


This because Python starts counting from 0 and not 1. :slight_smile:

In order to get exact coords from the user input, subtract one to the user's column and row indicating where to put the X, as well as to ship's column and row.


len() starts counting at one? What you mean is that indexes (list, strings) start counting at zero

no? exactly the opposite, we start counting at one and the list starts at zero, so you should subtract one


I mean in this case, If you input (1,1) the X will be assigned at (0,0). Am I wrong?


no, but this means you have to subtract one, not add one to the user input

If you do this, even your not in range(5) is perfectly validated


Oh sorry, yea you're right. I mixed the two. Corrected my first post.


I still don't get it.
let's take a look at my code:

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)


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

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

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input("Guess Row:")) - 1
guess_col = int(raw_input("Guess Col:")) - 1

print ship_row
print ship_col

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"

As you said I subtracted the user input by one, but I still got the same problem.


oh, i think i understand.

ship_row and ship_col are the actually coordinates of the ship

the X on the board marks a missed guess, not the ship coordinates


yes exactly! I can't figure it out!
Actually I think we should replace the [guess_row][guess_col] with [ship_row][ship_col], am I right?


in one of the next few exercises we add a loop, giving the user multiply guesses

By marking the users wrong guesses as X on the board, the user knows what coordinates not to guess again.

the ship_row and ship_col are just for debug purposes, so we know where the ship is actually is, this is useful for us while building the game. The user playing the game never gets to see the ship coordinates till the end after they run out of guesses, and lost (or, if they actually guess the ships coordinates)

No, most certainly not


Marking the wrong guesses with X also allows us to check if the user doesn't enter coordinates they guessed before. This will be the task in the next exercise, then the exercise after that, you will add the loop, hopefully then everything comes together for you