Cant sink my own ship!


#1

I'm very agitated right now!!! this is the battleship game and I'm on the part where if you guess incorrectly and "X" is placed on the board.
Here is my code for that portion (i'm not gonna write the entire code):

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
else:
    print "You missed my battleship!"
    board[guess_row,guess_col] = "X"
    print_board(board)

If somebody could tell me what it is i did wrong i would appreciate it very much.
Here is the error I keep getting
Traceback (most recent call last):
File "python", line 33, in
TypeError: list indices must be integers, not tuple
Tuple means two numbers but I'm just not getting it.


#2

Hi,

this is the problem. To access a value in a 2-dimensional list use:

my_list[index1][index2]


#3

i fixed that issue but later after incorporating a code where if you've already chose previous coordinates you get a message saying "you guessed that one already". I get an error message saying List index out of range and addresses line 34 which is the elif statement. What is wrong with it?

    print "Congratulations! You sank my battleship!"
else:
    if guess_row > range(5) or guess_col > range(5):
        print "Oops, that's not even in the ocean."
34.    elif board[guess_row][guess_col] == "X":
        print "You guessed that one already"
    else:
        print "You missed my battleship!"
        board[guess_row][guess_col] = "X"
        print_board(board)

ts not the indentation, that line slightly moved over b/c i put "34" next to it


#4

after playing around i realized that every time i enter a pair of coordinates that are out of the 5x5 grid i get an error message saying list index out of range but if i enter coordinates that are withing the 5x5 grid then everything is fine. So i'm assuming there is something wrong with my elif statement but i dont see anything wrong with it.


#5

This should be guess_row > 4, or guess_rown not in range(5). Don't use > or < to compare integers to lists. It won't raise an error, but can behave strangely.


#6

You were right. I used the "<" ">" so thats why it was coming up with errors. Thank you.