Printing guess in the wrong spot


#1

It says that my code is incorrect but when I guess a number, it appears in a spot +1 of what I guessed in the row and column. board[guess_row-1][guess_col-1] should be minus 1 because the numbering in the list starts at zero.

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!"
print_board(board)

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


for turn in range(4):

    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
    
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
    else:
        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-1][guess_col-1]** == "X"):
            print "You guessed that one already."
        else:
            print "You missed my battleship!"
            board[guess_row-1][guess_col-1] = "X"
    
    turn = 0
    turn = turn + 1
    print "Turn %s" %(turn)
    
    print_board(board)


#2

lists are zero indexed based, humans start counting at one, there is the difference

if you want to compensate for human vs computers counting, do this before:

if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):

otherwise, if the user enters 0 you will get an index error and the user can't guess 5 even though they need it now


#3

I know, I did that in my code to account for the difference in counting, the problem is that codecademy is saying that it's incorrect


#4

and i am telling you do the subtraction to compensate for this difference at the wrong spot

Which specific exercise are you at?

here:

turn = 0

why would you do this? turn gets defined in the for loop, not sure why you would overwrite it


#5

mmm okey I see what you mean, thanks!

Yeah, I figured out the turn part was wrong as soon as I ran it afterwards and fixed it haha


#6