# SyntaxError: invalid syntax

#1

"File "python", line 26
def random_col1(board):
^
SyntaxError: invalid syntax"

I don't really know what's going on. Why is def invalid syntax there, but not anywhere before?

from random import randint

board = []

for x in range(10):
board.append(["O"] * 10)

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

print "Let's play Battleship!"
print_board(board)

def random_row0(board):
return randint(0, len(board) - 1)

def random_col0(board):
return randint(0, len(board[0]) - 1)

def random_row1(board):
return randint(0, len(board) - 1)
if randint(0, len(board) - 1) == random_row0:
return randint(0, len(board - 1)

def random_col1(board):
return randint(0, len(board[0]) - 1)
if randint(0, len(board[0]) - 1) == random_col0:
return randint(0, len(board[0]) - 1)

ship_row0 = random_row0(board)
ship_col0 = random_col0(board)

ship_row1 = random_row1(board)
ship_col1 = random_col1(board)

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

if guess_row == ship_row0 and guess_col == ship_col0:
print "Congratulations! You sunk my battleship!"
print "Turn", turn + 1
break
else:
if (guess_row < 0 or guess_row > 10) or (guess_col < 0 or guess_col > 10):
print "Oops, that's not even in the ocean."
print "Turn", turn + 1
elif(board[guess_row][guess_col] == "X"):
print "You guessed that one already."
print "Turn", turn + 1
elif guess_row == ship_row1 and guess_col == ship_col1:
print "You sunk my aircraft carrier!"
print "Turn", turn + 1
break
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)
print "Turn", turn + 1
if turn == 3:
print "Game Over"

#2

It's pointing at where it stops making sense, so the problem is either where it points or earlier

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.