A Real Win!


#1

https://www.codecademy.com/courses/learn-python/lessons/battleship/exercises/a-real-win?action=lesson_resume

I passed this exercise, but when I tried the code the first time with the break inside of the if statement I got this error:
SyntaxError: ‘break’ outside loop.
with this code:

 else:
    if (guess_row.isdigit()) and (guess_col.isdigit()):
      guess_row = int(guess_row)
      guess_col = int(guess_col)
      if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sank my battleship!"
        break
      else:
        if guess_row not in range(5) or \
          guess_col not in range(5):
          print "Oops, that's not even in the ocean."
        elif board[guess_row][guess_col] == "X":
          print "You guessed that one already."
        else:
          print "You missed my battleship!"
          board[guess_row][guess_col] = "X"
        if turn == 3:
          print "Game Over"

The correct code below has the break in the correct place, but I’m wondering why the break needs to be in the outer if statement. Shouldn’t the “break” be within the if statement that gives the “Congratulations message?”

  else:
    if (guess_row.isdigit()) and (guess_col.isdigit()):
      guess_row = int(guess_row)
      guess_col = int(guess_col)
      if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sank my battleship!"
      break
      else:
        if guess_row not in range(5) or \
          guess_col not in range(5):
          print "Oops, that's not even in the ocean."
        elif board[guess_row][guess_col] == "X":
          print "You guessed that one already."
        else:
          print "You missed my battleship!"
          board[guess_row][guess_col] = "X"
        if turn == 3:
          print "Game Over"

#2

Hi @hypnagogia3,

To fully diagnose the problem, we need to see the code that precedes the samples that you posted, since it relates to the structure of the for loop…

This, from your first code sample, conforms to the correct logic, since, as you have noted, the break should follow and accompany the "Congratulations ..." message …

      if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sank my battleship!"
        break
      else:

This, from your second code sample, does not appear to be syntactically correct, since the break intervenes between the if and the else block headers by being on the same indentation level with them …

      if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sank my battleship!"
      break
      else:

To determine the cause of the SyntaxError, we would need to look at the original code, including the for loop header, so that we could see why the Python interpreter considered the break to reside outside the loop.


#3

This is the code that I think is correct. For some reason it was not allowing me to move forward in the exercises unless I added break in the outer if statement.

from random import randint

board = []

for x in range(0, 5):
  board.append(["O"] * 5)

def print_board(board):
  for row in board:
    print " ".join(row)

print_board(board)

def random_row(board):
  return randint(0, len(board) - 1)

def random_col(board):
  return randint(0, len(board[0]) - 1)

ship_row = random_row(board) * 3
ship_col = random_col(board) * 3
print ship_row
print ship_col

# Everything from here on should be in your for loop
# don't forget to properly indent!
for turn in range(4):
  guess_row = raw_input("Guess Row: ")
  guess_col = raw_input("Guess Col: ")
  if (guess_row == " " or guess_row.isalpha()) or (guess_col == " " or guess_col.isalpha()):
    print "Invalid Entry"
  else:
    if (guess_row.isdigit()) and (guess_col.isdigit()):
      guess_row = int(guess_row)
      guess_col = int(guess_col)
      if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sank my battleship!"
        break
      else:
        if guess_row not in range(5) or \
          guess_col not in range(5):
          print "Oops, that's not even in the ocean."
        elif board[guess_row][guess_col] == "X":
          print "You guessed that one already."
        else:
          print "You missed my battleship!"
          board[guess_row][guess_col] = "X"
        if turn == 3:
          print "Game Over"
  print_board(board)
  print "Turn",turn + 1
  # print turn + 1 here

#4

This if block is contained within the for loop, therefore it is permissible for it to contain the break statement …

      if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sank my battleship!"
        break

#5

Thank you for your help! :slight_smile:


#6