Battleship 16/19 New if under the first else or the second else?

#1

In this lesson they ask us to:

Put it after the if/elif/else statements that check for the reason the player missed. We want “Game Over” to print regardless of the reason.

And the Hint tells us:

Your new if should go under the else that handles all of the incorrect guesses, but outside of any of the nested conditions. This is because it doesn’t matter why the guess is wrong; after 4 wrong guesses, the game is over.

So intrepret that i should put the new IF under the first ELSE like this:

``````  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."
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)
if turn == 3:
print "Game Over"
``````

And everything goes smooth…BUT…when I look at the Codecademy solution, they put the IF under the second ELSE like this:

``````  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."
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)
if turn == 3:
print "Game Over"
``````

But why, I thought they clearly stated:

Your new if should go under the else that handles ALL of the incorrect guesses

Can somebody explain which one is correct? I thought my way was the right way, but…

#2

in this case under means nested inside. Although the instructions are correct, the exercise could have better choice of words

Put it after the if/elif/else statements that check for the reason the player missed.

which makes it a bit clearer.

but thinking about it, codecademy solution make more sense. In your code, you check if its game over (lost) even after the user has already won. Which seems a bit unnecessary

#3

Oh ok, I see, then I’ll place it under the second else. Thanks once again!

#4

understanding how the codes differ, what influence the difference has on the program and its logic is far more important. Then you should be able to figure out the placing.

which is more valuable then just following a bunch of instructions. I don’t mean that in a harsh way

You could have asked yourself: does placing `"game over"` here make sense? That is a much more vital question

#5

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