How should my conditional statements be nested?

Question

How should my conditional statements be nested?

Answer

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!

2 Likes

for those who come because the console return “be sure to print bla bla bla bla”. I lost 10 minutes because i forgot a dot in the string of the print function. Its has to be exactly the same text to be validate as correct.

Cheers

2 Likes

what does list index out of range mean? here is my code:
else:
print “You missed my battleship!”
board[guess_row][guess_col]=“X”
print_board(board)
if guess_row not in range(6) or guess_col not in range(6): #i looked at hint for this line
print “Oops, that’s not even in the ocean.”
WHERE IS MY MISTAKE?>???

oh never mind lol that was supposed to happen XD

Please I have a problem. I wrote well the code but it still displays an error


How to continue ?

1 Like

you used a “!” insted of “,” on the Oops word

Should it specify that we need to delete and move the first else statement below the new if? Because it does not work if you don’t…

My problem was about letter… too much “O” in string… i wrote “Ooops”.
Correctly is “Oops”… :crazy_face: