17. A real win


#1

okay, i have done everything right, but its the break that's getting me. I've tried every single indent that made sense. what am i doing wrong? This is my code.
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"
for turn in range(4):
print "Turn", turn + 1
print_board(board)
if turn == 3:
print "Game Over"


#2

If you're getting something like "break outside a loop" then indeed a break makes no sense outside a loop. So you'll have to decide if part where your break is should be moved into a loop, or if the break shouldn't be there at all.

Make sure to post intact code if you want others to read it, and always explain what is happening and what you want to happen instead. If you make people guess by not providing this information then you lessen the chance that people will respond and if you do they might not even be spending their time on what you wanted help with


#3

I had the same problem. I fixed it by changing the 'if' statement to a 'while' statement and it now works.


#4

I posted my code at the top of the page, and thanks. :smile:


#5

I got it to work. Not sure why the code didn't work before. Any ideas?


#6

It worked!!! Amazing!!!


#7

my answer is :

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

turn = 0
for turn in range(4):
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 == 5) or (guess_col == 5):
        print "Oops, that's not even in the ocean."
    elif(board[guess_row][guess_col] == "X"):
        print "You guessed that one already."
    else:
        if turn == 3:
            print "Game Over"
        print "You missed my battleship!"
        board[guess_row][guess_col] = "X" 
    print "Turn", turn
    print  "Turn", turn + 1
    print_board(board)