Battleship Bad Aim Bug?


#1

Is this a bug

When my code is


if guess_row == ship_row and guess_col == ship_col:
     print "Congratulations! you sank my battleship!"

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

    else:
         print"You missed my battleship!"
         print print_board(board)
         board[guess_row][guess_col]="X"

print print_board(board)

The code passes; however, in testing about the range of 5 it doesn't recognize it.

When code like this it is correct

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! you sank my battleship!"

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

else:
    print"You missed my battleship!"
    print print_board(board)
    board[guess_row][guess_col]="X"

print print_board(board)

#2

Hi @tallishpuppy7,

It is not a bug! In your first example you are indenting your elif and else under the if condition and this is a syntax error according to the Python Doc :smile:


#3

ok so the elif is starting a new statement that doesn't need to follow the if statement it can be it's own.
Thanks for the link (saved).


#4

@tallishpuppy7, my understanding is that elif and else are part of if as in that they cannot be used without if but they should not be indented "inside" or if you prefer "under " if :smile: