10 You win

Hey people I keep getting syntax error and I don’t know why?

my code;
from random import randint

board =

for x in range(0, 5):
board.append([“O”] * 5)

def print_board(board):
for row in board:
print " ".join(row)

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)
guess_row = int(raw_input(“Guess Row:”))
guess_col = int(raw_input(“Guess Col:”))

print ship_row
print ship_col

Write your code below!

if guess_row == ship row and guess_col == ship_col:
print “Congratulations! You sank my battleship!”

Have you indented your print statement?

Yes i have… i can not find mistake

Please format your code as it is most likely an indentation issue.

1 Like

What I get : 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 29)

from random import randint

board = []

for x in range(0, 5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)

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)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print ship_row
print ship_col

# Write your code below!
if guess_row == ship row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
1 Like

I don’t know if you’re still stuck on this problem, but what I did was switch around the guess_row == ship_row and the guess_col == ship_col to where the guess_col statement came first.

1 Like

Your code looks fine…This code run for me, cross reference to spot any error in your code.
from random import randint

board =

for x in range(0, 5):
board.append([“O”] * 5)

def print_board(board):
for row in board:
print " ".join(row)

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)
guess_row = int(raw_input(“Guess Row:”))
guess_col = int(raw_input(“Guess Col:”))

print ship_row
print ship_col

Write your code below!

if guess_row == ship_row and guess_col == ship_col:
print “Congratulations! You sank my battleship!”

I found it! You wrote if guess_row == ship row

it should be ship_row and not ship row!

U welcome Hope it works!

DELETE YOUR OLD CODE AND WRITE THE BELOW(copy and paste if you like!!!):
_from random import randint_
_board = []_
_for x in range(0, 5):_
__ board.append([“O”] * 5)****
_def print_board(board):_
__ for row in board:****
__ print " ".join(row)****
_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)_
_guess_row = int(raw_input(“Guess Row:”))_
_guess_col = int(raw_input(“Guess Col:”))_
_print ship_row_
_print ship_col_
_if guess_row ==ship_row and guess_col == ship_col:_
__ print “Congratulations! You sank my battleship!”****

@xkyang33,

Please read the, We do not tolerate cheating! Section.
https://discuss.codecademy.com/guidelines

Thanks.

2 Likes