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
caders
February 23, 2016, 4:37pm
4
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!”** **
caders
October 23, 2016, 7:36pm
11
@xkyang33 ,
Please read the, We do not tolerate cheating! Section.
https://discuss.codecademy.com/guidelines
Thanks.
2 Likes