Battleship logic error in if statement


#1
def checker(guess_row, guess_col):
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sank my battleship!"
    elif guess_row != (0 or 1 or 2 or 3 or 4) or guess_col != (0 or 1 or 2 or 3 or 4): 
        print "Oops, that's not even in the ocean."
    elif board[guess_row] or board[guess_col] == 'X':
        print "You guessed that one already."
    else:
        print "You missed my battleship!"
        board[guess_row][guess_col] = 'X'
checker(guess_row, guess_col)

Every time I run this function I get the output "Oops, that's not even in the ocean."

My code isn't exactly like the instructions explained because I have been messing around with it for a while trying to get this to work. No matter my inputs for the guesses, I get the same output. Does anybody know what I am doing wrong?


#2

This will always be True:

     (0 or 1 or 2 or 3 or 4)

We have two useful tools in Python... in and range().

elif guess_row not in range(len(board)) or guess_col not in range(len(board[guess_row])):

which one supposes might be easier to read as nested if.

elif guess_row not in range(len(board)):
    if guess_col not in range(len(board[guess_row])):

Needs testing. I'm just writing this off the cuff.

The above are both row indexes. Should be,

elif board[guess_row][guess_col] == 'X'

After you do the fixes, please post your code again, and include a link to the exercise. Thank you.


#3

Here's my new code:

`if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sank my battleship!"
else:    
    if guess_row not in range(len(board)) or guess_col not in range(len(board)): 
        print "Oops, that's not even in the ocean."
    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'`

I fixed the first issue with:

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

Now my program is skipping over the elif statement. When I had my previous version in, the elif statement was running regardless if it was supposed to be true or not.

Could you please explain the difference between these two?

Here is where the nested if statement begins: https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/1/2?curriculum_id=4f89dab3d788890003000096#
It continues in the next two lessons after.


#4

    [0] [0,1,2,3,4]
    [1] [0,1,2,3,4]
    [2] [0,1,2,3,4]
    [3] [0,1,2,3,4]
    [4] [0,1,2,3,4]
    [ ] [         ]
     ^       ^
     |        \
    row      column

Remember that guess_row and guess_col are only integers. Just because the name is guess_col does not mean it refers to a column automatically. It must appear in the second subscript.


#5

This is a mistake we most of us make. The lesson you point to has us create an if-else statement. This lesson has us insert a couple more conditionals. What is not immediately obvious is that we can push down the earlier else statement and insert two elif's.

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
elif guess_row not in range(len(board)) or guess_col not in range(len(board[0])):
    print "Oops, that's not even in the ocean."
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'`

#6

Okay I seemed to have fixed any errors. Thanks you your help.