17/19 - break problem


#1

Can someone help with my break?
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"
for turn in range(0, 4):
print "Turn", turn + 1

print_board(board)


#2

Hey! Soooooo your problem is most likely indentation!

I'm guessing that you have not indented "if guess_row" soo that it is nested (I believe that is the word?) inside of your "for turn in range" loop.

You cannot break a loop that is not a loop..


#3

Thanks it works but then after that there won't be proper indentation for the other else and elif clauses.


#4

Well then go ahead and fix those!
There is a reason for indentation! It keeps levels of idea in your code!!

Paste out all your code(with new fix) and maybe we can see where the error is!


#5

I still can't do it.
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"
if turn == 3:
print "Game Over"
for turn in range(4):
print "Turn", turn + 1

print_board(board)


#6

this was perfect feedback.

the instructions doesn't even mention needing to have all of the game logic inside of the 'for' loop in any prior lesson.


#7

I think that part could've been clearer, and based on all the Q&A on this exact issue I believe many other people would agree!

Lesson 15 does call out the requirement to have the logic in a for loop, though it's subtle and in a code comment (who reads those things!):

Everything from here on should go in your for loop!
Be sure to indent four spaces!

That said, it wasn't clear to me when I read through lesson 15 that the for loop was a requirement. I used one in lesson 15 because I didn't think there was any other way to loop through 4 attempts, not because I noticed the note.


#8

I didn't notice that in lesson 15.

The issue is that all of the other lessons including 15 work fine without it being in the loop. So you are a couple lessons down the road before you know something is wrong.

Thanks again. Working on Loops now.