Why do I get a “break outside of loop” error?


#1

Question

Why do I get a “break outside of loop” error?

Answer

This will happen if your indentation is incorrect. Be sure you’ve indented your code inside of your for loop. Everything that happens during a turn needs to go inside of the loop.
Then, inside of the if statement that is inside of the for loop, write the break after telling the user they hit the battleship.

for turn in range(4):
  .
  .
  .
  if guess_row == ship_row and guess_col == ship_col:
    print "Congrats!"
    # BREAK GOES HERE
  .
  .
  .

FAQ: Learn Python - Battleship - A Real Win