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:
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?>???
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?
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).