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!


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.



what does list index out of range mean? here is my code:
print “You missed my battleship!”
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.”

1 Like

oh never mind lol that was supposed to happen XD

1 Like

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

How to continue ?


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:

You have to do like this.
You missed some lines.


Thanks. I notice that I’ve forgotten the lines from 8 to 12

I have a prob with this " \ " symbol. Whenever I use this it should be in white but is instead displayed in red and the console tells me that is an error? Can someone please help me?

Dude someone to answer me please!!!

Thanks! I was struggling with the same thing. In my case it was the comma I left out!


This is the code i typed and it works alright. After you type \, you press enter immediately and continue with the code for the if condition.

Hope this helps although it’s quite late.

Thanks alot for this buddy

1 Like