X in grid off by +1?


if i do not add the (-1)'s to the raw_input of the guesses the grid displays the 'X' in wrong place (1 space to the right and down). I get no error messages either way, and the course lets me pass either way as well.

am I crazy? or is this a bug?


from random import randint

board = []

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

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

print "Let's play Battleship!"

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

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

ship_row = random_row(board)
ship_col = random_col(board)
#print ship_row
#print ship_col

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4):
    print "Turn", turn +1, " of 4"
    guess_row = int(raw_input("Guess Row:")) -1
    guess_col = int(raw_input("Guess Col:")) -1

    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
        if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
            print "Oops, that's not even in the ocean."
        elif(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"
        if turn == 3:
            print "Game Over"
    # Print (turn + 1) here!
    print turn + 1


well, lists are zero indexed based. The exercise just assumes you are fine with it

What you did is actually make the game more user friendly, you bridge the gap between the user expecting to enter 1-5 and the list which is 0-4


right. thanks. i guess i missed where it said the input was to be in 0 - 4 format.


well, if you know that lists are zero indexed based, you can know it

also, range(5) is a big hint, since range does not include the stop value (5 in this case), and the default start value for range is zero

The hints are there, you just need to see them


yup. get it. i just went about playing the game as someone ignorant of how it was programmed. and then went back into the program to "fix" it. my concern is that the course let me proceed with and without the (-1)'s.


that is proper testing of your code, good job :slight_smile:

Yep, the exercise doesn't cover this. Choices had to be made

For example, someone who doesn't programmer might be so "dumb" to not enter a number when prompted, see what happens when you do that


thanks @stetim94,
i better get back to the lessons, as it seems this course will be deleted in the next few days, and my progress will be lost!
hope i can finish it!


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