17/19 - why does "break outside loop" come up


#1

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!

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_board(board)
for turn in range(4):
print "Turn",turn+1
if turn == 3:
print "Game Over"


#2

Just add "abc = [1]" at the top then make a for loop only holding break


#3
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

for turn in range(4):
    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"
    if turn==3:
         print 'Game Over'

print 'Turn', turn+1
print_board(board)

ANSWER


#4

Inserting the "break" in the same place(directly under print, have tried adjusting indentation) but am still getting the: File "python", line 33
SyntaxError: 'break' outside loop.
Help would be appreciated.


#5

I also have this error and tried different options, can anyone help? it would be appreciated


#6

@felicity_johnson_mai @creebler @ana_catarina

Break is used exclusivly to break a loop, that is all there is to it.

You use it like this.

for example in range(500):
    if example == 50:
        break

@betaace13272

If you are going to post code please tell them what they did wrong and just post the code bit that is wrong in a corrected format. If they do not understand what is going on posting the whole chunk will not make it any easier for them.


#7

Thank you for replying zeziba. I was able to pass the lesson but am not exactly sure what I did as previous attempts to adjust the indentation on the break would return the same error, which led me to believe the issue was actually somewhere else in the script.


#8

I went and researched the archived Q&A feed. What fixed mine (and i'm betting many others need this) is the level of indentation needed. I've ready a bunch of people saying "It's the indent" "Its the indent." So I would indent break, but that isn't it.
It ends up being the ENTIRE indentation of everything below for turn in range...... EVERY line's indentation should be based off of that for loop. Nothing should be at the same level of it. If you have noticed (as I did, but couldn't figure it out) why the Game Over or the Not in the ocean message wasn't displaying. Chances are, this is the fix for that too. Hopefully it will help.

If it will help, I have include the code starting with the for loop. Just to make sure you can see it visually.

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:
    break
    print "Congratulations! You sunk my battleship!"
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"
if turn == 3:
    print "Game Over"
    # Print (turn + 1) here!    
print_board(board)

#9

Hi,

I'm still not getting it, even with the examples from above, here is my code... Could anyone explain what I'm doing wrong this is driving me crazzzy and I feel like i've tried everything.

if guess_row == ship_row and guess_col == ship_col:
    break
    print "Congratulations! You sunk my battleship!"
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!
for turn in range(4):
    print "Turn",turn+1
    if "Turn" == 3:
        print "Game Over"
    print_board(board)

#10

Take this,

if guess_row == ship_row and guess_col == ship_col:
    break
    print "Congratulations! You sunk my battleship!"
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!
for turn in range(4):
    print "Turn",turn+1
    if "Turn" == 3:
        print "Game Over"
    print_board(board)

Turn it into this,

for turn in range(4):
    guess_row = raw_input("Guess a number")
    guess_col = raw_input("Guess a number")
    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+1
        if "Turn" == 3:
            print "Game Over"
        print_board(board)

#11

Thank you, it works now...
not really sure why, but it does!


#12

It works when I use this code, but it doesn't print "Congratulations! You sunk my battleship!"


#13

break has to come after "Congratulations! You sunk my battleship!"


#14

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):
guess_row = raw_input("Guess a number:")
guess_col = raw_input("Guess a number:")
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 + 1
    if "Turn" == 3:
        print "Game Over"
    print_board(board)

When it prints:
O O O O O
O O O O O
O O O O O
Guess a number: 2
Guess a number:
Oops, that's not even in the ocean.
Turn 2
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess a number: 5
Guess a number: 3
Oops, that's not even in the ocean.
Turn 3
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess a number: 4
Guess a number:
Oops, that's not even in the ocean.
Turn 4
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
None

But it's not printing "Congratulations! You sunk my battleship!", "You guessed that one already.", "You missed my battleship!", and "Game Over"


#15

Well thank you Zeziba....this is the best example and it worked...my indents were off. I looked at other examples and they didn't work but your indents where perfect. Thank you for helping.


#16

And just for the record you do not realize your indents are off until you get to the print statement at the bottom because everything will work up until you add that last print statement. Then you realize you have to go all the way back up to fix it.:smile: