13/19 [solved]


#1

This is my code:

if guess_row == ship_row:
if guess_col == ship_col:
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."
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
elif:
X == X (??? I'm pretty sure this is wrong but I have no better ideas)
print "You guessed that one already"
print_board(board)

Error Message:
File "python", line 38
elif:
^
SyntaxError: invalid syntax

I have moved it back and forth and I have no idea where it's supposed to go .-. sorry and thanks!


#2

From the hint in 13/19 this is what I wrote:
elif board[guess_row][guess_col] == "X":

My code works. It seems that the elif likes a statement next to it, not just elif:


#3

Sorry I still get this message though:

File "python", line 38
elif board[guess_row][guess_col] == "X":
^
SyntaxError: invalid syntax


#4

@jollieollie10

Look at the line before that, if you have bad indentation or missing a closing end ie ],},) it can cause that error.

Also elif is just like an if you HAVE to compare something for it to work.

if True:
    return True
elif False:
    return False

#6

@jollieollie10

I would change a few things to make it easier to read.

if (guess_row == ship_row) and (guess_col == ship_col):
    print("Congratulations! You sank my battleship!")
else:
    if (guess_row or guess_col) not in range(5):
        print("Oops, that's not even in the ocean.")
    else:
        print("You missed my battleship!")
        board[guess_row][guess_col] == "X"

#7

I managed to solve the problem, the elif statement is supposed to be after the if statement and before the else statement. So the full code looks like this:

if guess_row == ship_row:
if guess_col == ship_col:
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][guess_col] == "X":
    print "You guessed that one already."

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

print_board(board)