Where am I supposed to write this elif to check if the coordinate has already been guessed?


#1

Question

Where am I supposed to write this elif to check if the coordinate has already been guessed?

Answer

Our code to this point (in pseudo code) looks like the code below. We want to insert an elif that checks if their wrong guess was one they’ve already made before.

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

You cannot have an elif without an if before it, so writing it after an existing else would be invalid, because else must be last. Be especially careful with your indentation as well, as the new elif should be on the same indentation level as the if that checks for the guess being invalid.


FAQ: Learn Python - Battleship - Not Again!
#2

2 posts were split to a new topic: What Does This Code Do?