Danger Will Robinson - X does not mark the spot


#1

My code goes through just fine but I notice is that the "X" that should be at row 3 column 4 is not at row 3 and column 4. Instead it is at row 4 column 5.

Another thing I noticed is that the program is assigning ships to row 0 which means the random number generator is not set up correctly even though codeacademy gave explicit code to use.

This is the code given to use

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

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

This is what i think the code should be

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

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

O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row: 3
Guess Col: 4
You missed my battleship!
O O O O O
O O O O O
O O O O O
O O O O X
O O O O O
5
None

I can technically move on but I'd rather not because this program seems messed up.
Any ideas?


#2

To resolve my issue I've decided to change
This:

guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

to this:

guess_row = int(raw_input("Guess Row:"))-1
guess_col = int(raw_input("Guess Col:"))-1

It solves my problem uf putting the "X" in the correct spot and it makes it possible to get a correct guess even though the random number generator provided gives a possible 0 row and column.

But I'm still peeved/curious about why I'm allowing a row and column of 0 to happen.


#3

If it's meant to pick a random location out of all of the board, then you'll need to use parameters for the randint in such a way that you get the correct range.

So figure out what ranges you want and then figure out how to specify that to the function and then use that.

Edit
Oh you mean labelling rows and cols 1-5 instead of 0-4... both ways are arbitrary, but starting at zero is less code. You could just as well use animal names.