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?>???

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 ?

2 Likes

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…

1 Like

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.

2 Likes

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!

Capture

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

Code is exactly the same as the solution in every single way, but getting “Program took too long to terminate”

image

OK… so after I copy the solution over, it throws out the same error

It might be a problem with how you’re getting user input. If you’re using Python 2, you should use raw_input(). In Python 3, you should use input(). If you’re still getting the error even after using the correct input function, try running the program locally or using another online service (sometimes code on CC doesn’t work properly when Python input is involved).

Refreshing the browser I was using sorted the problem :slight_smile:

1 Like