A real win


#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"
if turn in range(3):
print "Game Over"
for turn in range(4):
print "Turn", turn + 1
print_board(board)
im getting: File "python", line 33
SyntaxError: 'break' outside loop


#2

Perhaps you'd like to ask a question. And if your code is relevant, make sure it's accurately represented in your post! Wouldn't want others to look at some other code than the one you're asking about!


#3

The same thing is happening to me...
My question is.. where do you put break?


#4

It's not where so much as when. When should you break?
Why do you need break at all? What does it do?

Gotta know what it's there for, can't just try to fit it in wherever it seems to be allowed!


#5

It said to put it after Congratualtions message and I did, then it didnt work

Please be more clear


#6

Well it might have said that, but that's of secondary importance. What matters is: what effect are we trying to get out of the break? Programming has to be deliberate. We have an idea and we need the computer to execute it. The computer doesn't know, we have to say what we want. To do that, we gotta know what it is we want.


#7

so would I say... break the loop??
Im so confused


#8

And I'd agree with that. So.. where's your loop? Surely all the code for a turn should be there. And you'd stuff in that break once that turn has decided the game is over.


#9

would it be,....

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


#10

No clue, that's not valid Python code anyhow.

Where does your loop start?
Where does it end?
Does that span contain the whole turn?
Where in there do you need to break?
done
Don't ask me where! If you can't answer the above questions then you need to figure that out. And if you can answer them then you know where it goes.


#11

or would it be

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


#12

Well, thank you for all the help you tried to give me. I really appreciate it! Have a good day!


#13

Like this then:

You should break the loop after guessing right. Cause the game is over. Yes? It shouldn't go anywhere else. So put it there and forget about the break.

And all the code for a turn should be in the loop, yes?

So if your code agrees with the above two things, then you most definitely should not get "break outside loop" because we just said it should be in a loop since it's part of a turn and the turn is in a loop.