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

"'break' outside loop" keeps on coming up and every time I try a suggestion from the forum a different error pops up. There's probably something I've missed.


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

You can't put a break statement there, what would it exit? The rest of the code will be skipped anyway once the if condition was met, because it is in the else part.

You should put the entire if/else thing into a loop, that you can exit when the ship is sunk.


#3

So, "break" will only exit loops that the code is inside of. Currently, break is not within any loops. You need everything from
guess_row = int(raw_input("Guess Row:"))
until
print "Game Over"
to be inside of a loop. You can place all of that in the for loop you have near the bottom, but you need to put that print statement below it somewhere within the else clause (wherever another turn would happen).


#4
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_board(board)
    if turn==3:
        print "Game Over"

I tried to put it in the for loop but it says it's still not in.


#5

So right now, you will ask them for their input 4 times, but only your last input will actually be used. This is because the for statement and everything inside of it will be executed until it ends. You need to use the input they give you as your comparisons every time . To do this, indent your if/else and everything inside of those. Everything you just replied with should be inside of that top "for" statement.


#6

Now the first else has invalid syntax.


#7

Is this how you have it setup (I simplified a lot)

for:
   guess_row/col = ....
   if:
       print
       break
   else:
       everything else

#8

Thank you for showing me that, the break and print were not aligned so I changed that. I also aligned elif and the 2nd else with the 2nd if statement and it worked.


#9

It took me a really long time to figure this one out but basically, I passed when I rearranged the indentations and formatting. Looking at other people's advice, I'm guessing it is because now everything is in the "for" loop. Anyway, here's what I got: