General questions about the code


Very confused with the code…

codecademy’s code prints the “X” 1 spot higher in the column and row on the printed board and also allows the ship to be placed off the board, at [0,0] for example.

Problems with original code?
–without adding " + 1" outside the randint to the function the boat can be off the board and not include column and row 5 since the range would be 0 - 4; with the edit the range is 1 - 5

–range(5) doesnt take input of 5 as on the board unless changed to range(6)

–without the " - 1" to the else statement to place the “X” on the board; the “X” is printed 1 spot higher than the user inputs

I made the following changes outside of codecademy in python and it seemed to fix some but not others.

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

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

                        if guess_row not in range(6) or guess_col not in range(6):
                                print ("Oops, that's not even in the ocean.")
                                print ("You missed my battleship!")
                                board[guess_row - 1][guess_col - 1] = "X"

I’m not really sure if I made it redundant since codecademy is my only experience with python. Can someone with more experience confirm this error and fix or am I reading the code wrong?

^^ attached image for my entire code and testing
[0,0] should return the “Oops, that’s not even in the ocean” line shouldnt it?


Arrays (lists) are zero-indexed, so 0,0 is a valid reference.

    print 5 in range(5)     # False


so is codecademy’s example flawed?

i changed the if statement that prints “oops, that’s not even in the ocean.” to

if guess_row not in range(1,6) or guess_col not in range(1,6):

and it seems to fix the [0,0] bug, but is there a better way?


Your range in both axes should be 5, not 6.

    if guess_row not in range(5) or guess_col not in range(5):
        print "Oops, that's not even in the ocean."


but then the index is [0,1,2,3,4]

from a user standpoint the board is laid out as columns and rows[1,2,3,4,5]

if i don’t make these changes the board prints the “X” 1 spot higher. For example, [1,1] would print at [2,2] on the board.


That’s the nature of Python (and most languages). If you are bent on letting the user input 1 thru 5, then you will have to offset their inputs to reflect the reality of the program.


so is codecademy’s example flawed? who would look at a grid and assume the first columns and rows are 0?

and you seem to know what you’re doing; would you be able to tell me if my changes the best way of fixing this problem?


No it is not flawed. As I said, it is the reality of the program (and language). You are putting way too much on this non-issue.


this is codecademys example without revisions

given you have to assume the first column and row are at “0” I see how it works.
but am I wrong in looking at the board and assuming that the first column and row should be 1?

looking at a 5 x 5 board, who assumes the range is 0 - 4 instead of 1 - 5?

edit: i see that others have had the same logical assumption in multiple threads with the one linked below providing the clearest solution

+1 to values kind of weird

and that simple fix is actually what i was looking for… i haven’t run through the entire lesson but it seems like a crucial step to be added if it’s not in the lesson


There is no need for it to be added to the lesson. This is only a scaffold, not a full fledged program, and the less that has to be explained the simpler for the learner to grasp the key points. I mentioned above that all you need do is adjust user inputs to match the needs of the program, and it isn’t rocket science at this point. I hope you’re not going to be nitpicking over non-issues in the rest of this course. You won’t run out of candidate material, but you will try others’ patience.


you’re right. it’s not needed for the purpose of the course. thank you for your time and explanation.