A Real Win


#1

please i need help here i do not know what is wrong with my code when i add the break ,i see it correct but it keeps showing me " File "python", line 33
SyntaxError: 'break' outside loop"


#2

please include your code, and preferable also the exercise url


#3

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

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

#4

#5

what you place in the for loop should be indented:

a = [1,2,3]
for x in a:
   print x

not the for loop itself

think what should be in the for loop (thus require additional indention)


#6

i cannot really understand you know i have been on thsis exercise for almost 2 hours please can you be more specific on how you want me to go about it please


#7

here:

    for turn in range(4):
# 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:"))

everything from here on should go in the loop, what you place in the loop should be indented with 4 additional spaces:

for turn in range(4):
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))

now, you can indent the rest of the script.


#8

This is it i did it as you said but i still could not get 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

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

#9

here:

else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):

after else there should also be additional indention. You do know how indention works in python and when to use it for nesting things inside a function/for loop/if/else and so on?


#10

i still cannot get it dear i am badly stock here


#11

i do not know if there is another way you could do this?


#12

please post an updated version of your code with markup:

Also include the error message


#13

it says
Oops, try again.
Your code looks a bit off. Check the Hint if you need help! Your
code threw the following error: invalid syntax (python, line 39)

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

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

#14

did you read the topic about markup i linked to?


#15

yes i just read it but still cannot figure out to go about


#16

please i can i see you your full code?


#17

we are nearly there, showing the answer would violate the guideines

this lines:

else:
        print "You missed my battleship!"
        board[guess_row][guess_col] = "X"

else should be after the elif you guessed that one already.


#18

ok so you mean i should indent this lines like that?


#19

no, you have to think how the line should be indented, walk through the logic of the game


#20

what about this
...else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X" ...