10 You win


#1

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!"


#2

Have you indented your print statement?


#3

Yes i have.. i can not find mistake


#4

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


#5

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)


#6

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!"

#7

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.


#8

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!"


#9

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

it should be ship_row and not ship row!

U welcome Hope it works!


#10

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 printboard(board):****
__ for row in board:****
__ print " ".join(row)****
_printboard(board)****
_def randomrow(board):****
__ return randint(0, len(board) - 1)****
_def randomcol(board):****
__ return randint(0, len(board[0]) - 1)****
_shiprow = random_row(board)****
_shipcol = random_col(board)****
_guessrow = int(raw_input("Guess Row:"))****
_guesscol = int(raw_input("Guess Col:"))****
_print shiprow__
_print shipcol__
_if guessrow ==ship_row and guess_col == ship_col:****
__ print "Congratulations! You sank my battleship!"****


#11

@xkyang33,

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

Thanks.


#13