# 17. A Real Win

#1

Oops, try again. Your code looks a bit off. Check the Hint if you need help! Your code threw the following error: 'break' outside loop (python, line 35)

I thought all I have to do was add the break statement right after the "Congratulation" message. I even indented several different ways. Any help would be appreciated.

``````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!
if turn == 3:
print "Game Over"
print_board(board)``````

#2

The error message is that you have a break outside a loop.

What does break do?
Is break allowed outside loops?
Is your break outside a loop?

What does a loop do?
Which parts of your code should be affected by that?
Where does a loop end?

#3

#4

Don't guess, find out! If you have those things figured out then it should be plenty clear what the problem is and what you should do about it!

So you'd want to look up break, either by going back to whatever exercise introduced it or googling it (you're better off googling)
If there's ever anything in your code where you can only guess what it does, then that's the next obvious thing to investigate - and you shouldn't really add anything in that you don't have a firm idea of what it does because you're describing what is supposed to happen (writing code) and if you don't know what it is you're saying .. well, it's not going to turn out well

Since loops are for repeating things you'd need to know what it is you want to be repeating, so that's another thing that's got to be cleared up, another obvious next thing to investigate

Where a loop ends, I meant which lines, which is the first and last thing that will be repeated as a loop runs, whatever code should be repeated needs to be within that.

#5

Thanks for leading me to the right directions. I ended up skipping this section and moved on to the Loops. After completing the Loops seciton, I was able to figure it out. You were right but I think the lessons is somewhat backwards. The logical code that was checking our work should not have allowed us to get to step 17 without having it properly indented. To those stuck in this section make sure to also indent guess_row and guess_col

#6

Indentation affects the meaning of the code, in this case the code wasn't valid python code so it had no meaning at all, it didn't do what it should have.

It most definitely should not pass.

The realisation that the turn should be in a loop is no small detail! That's a very important part of the game.

Perhaps as you suggest the exercises are out of order? But that's not something I have want to look at or comment on cause it leads nowhere.

#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.