Battle Ship Bad Aim - 2 Else Statement? How


#1

In the following block of code which is correct it uses 2 else statement right after one another.

if guess_row == ship_row and 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"
print_board(board)

I thought the flow control is:

if
elif
else

So I guess my question is, you can stack more than 1 else statements on top of each other? Why is the second not an elif statement?


#2

We cannot stack else. There can be only one in any if statement.

if
elif
elif
else

We cannot tell from your code, but if it looks something like this,

if ____:
else:
    if ____:
    else:

then the two if’s are not stacked up but in their own if statement.


#3

Makes perfect sense. Thanks!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.