If statements


Below you will see the code I wrote for the set of ‘if statements’ for the battleship game. I was quite happy with it but surprised to find that codeacademy wants me to embed an ‘if statement’ within an ‘else statement’. I think that looks messy and confusing but I’m sure there must be a good reason to do that. Could someone please enlighten me? Is it incorrect to use multiple ‘elif statements’ one after the other?

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."
elif board[guess_row][guess_col] == "X" :
  print "You guessed that one already."
  print "You missed my battleship!"
  board[guess_row][guess_col] = "X"


Hi @thenike,

The organization of your if, elif, and else blocks is fine so far as the Python interpreter and the logic of the game are concerned, and easier to read than the nested organization that Codecademy suggests for this exercise. While you may have used an if statement within an else statement to pass Codecademy’s submission correctness test (SCT) , you may reorganize your code later on, if you would like to do so, especially in the final exercise where you are invited to “add on to your Battleship! program to make it more complex and fun to play”.