Why does the 'break' piece do this all the time?


#1

This is my code:
from random import randint

board = []

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

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

print "Let's play Battleship!"
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)
ship_col = random_col(board)
print ship_row
print ship_col

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4):
print "Turn", turn + 1
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sunk my battleship!"
break
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
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"
# Print (turn + 1) here!
print "Turn", turn
print "Turn", turn + 1
if turn==4:
print "Game Over"
print_board(board)

No matter what I try, I always end up with the "File "python", line 35
SyntaxError: 'break' outside loop" error message.

What is going gone? Please use a code in the response.


#2

It's likely because your indentation for the command is wrong, but I couldn't say for sure. It's pretty difficult to debug code that's not formatted - clicking on that </> symbol in the toolbar or typing Ctrl+K will make it significantly easier for others to read.


#3


#4

My code worked when I put break on Line 25


#5

You've got some code that represents a turn in the game, right?

All of that should run repeatedly.

So all of that should be inside a loop.

Indent the part of your code that should be in the loop, such that it becomes part of the for-loop


#6

you have to indent every row from line 25 and under so that it becomes part of the for loop.

for turn in range(4):

guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
print "Turn",turn + 1
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sunk my battleship!" 
    break   
else:
    if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
        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"

#7

for turn in range(4):
indent preformatted text by 4 spacesprint "Turn", turn + 1
indent preformatted text by 4 spacesguess_row = int(raw_input("Guess Row:"))
indent preformatted text by 4 spacesguess_col = int(raw_input("Guess Col:"))

indent preformatted text by 4 spacesif guess_row == ship_row and guess_col == ship_col:
indent preformatted text by 4 spacesprint "Congratulations! You sunk my battleship!"
indent preformatted text by 4 spacesbreak
indent preformatted text by 4 spaceselse:
indent preformatted text by 4 spacesif (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
indent preformatted text by 4 spacesprint "Oops, that's not even in the ocean."
indent preformatted text by 4 spaceselif(board[guess_row][guess_col] == "X"):
indent preformatted text by 4 spacesprint "You guessed that one already."
else:


#8
for turn in range(4):
    print "Turn", turn + 1
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))

    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
        break
    else:
        if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
            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"
        # Print (turn + 1) here!
        print "Turn", turn
        print "Turn", turn + 1
        if turn==4:
            print "Game Over"
        print_board(board)

#9

I had to copy and paste your code to pass. It said "You missed my battleship!". I hope the code is right.
from random import randint

board = []

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

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

print "Let's play Battleship!"
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)
ship_col = random_col(board)
print ship_row
print ship_col

Everything from here on should go in your for loop!

Be sure to indent four spaces!

for turn in range(4):
print "Turn", turn + 1
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sunk my battleship!"
    break
else:
    if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
        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"
    # Print (turn + 1) here!
    print "Turn", turn
    print "Turn", turn + 1
    if turn==4:
        print "Game Over"
    print_board(board)
# Print (turn + 1) here!
print "Turn", turn
print "Turn", turn + 1
if turn==4:
    print "Game Over"
print_board(board)