I was having trouble with the coding for the "You guessed that one already."
else if statement.
I found a solution but as I go back to add comments, I'm not certain WHY it worked.
Problem one - thinking too much.
if guess_row == ship_row and guess_col == ship_col: #hit print "Congratulations! You sank my battleship!" else: if guess_row not in range(5) or guess_col not in range(5): print"Oops, that's not even in the ocean." ** elif board[guess_row] == board["X"] or board[guess_col] == board["X"]: ** print "You guessed that one already." else: print "You missed my battleship!" board[guess_row] = "X" board[guess_col] = "X"
This returned the error: list indices must be integers, not str
After a number of permutations such as:
guess_row == board["X"] board[guess_row] == ["X"] board[guess_row] == int(board["X"])
I settled on:
elif board[guess_row] == "X" or board[guess_col] == "X": print "You guessed that one already."
The guess variables were integers already:
guess_row = int(raw_input(" ")) guess_col= int(raw_input(" "))
Which suggests that "X" was the offending string and that it was now being read as an integer, but I have no idea why.
Is it that I referenced "X" in brackets ["X"] or as part of board(["X"]) that it kept returning a string?
Did removing the brackets have the loop return an integer (the index number instead of the string?)
Thank you in advance for any help offered.