How should my conditional statements be nested?



How should my conditional statements be nested?


Nesting the ifs and elses for this project can be a bit tricky if you aren’t careful. Try to think of it in steps of what we’re trying to accomplish. The pseudo-code below may help:

if guess is correct:
  print “Congrats! You sunk my ship.”
else, not correct:
  if guess is invalid:
    print “Not in the ocean”
  else, just missed:
    print “Missed!”
    update element in board to be “X”
  print the board

An important part of this control flow is the order in which we check things. Notice how we never use the coordinates given if they are invalid, because that would cause errors in our program. We only use the coordinates if they are within the range of our list, even if the user has bad aim!

FAQ: Learn Python - Battleship - Bad Aim