mardi 15 août 2017

What's wrong with my python code (see code)?

I keep getting this message- elif board[gr][gc])=='X': ^ SyntaxError: invalid syntax

when running the code below (the specific line is starred). It says the elif statement has invalid syntax but I can't seem to find anything wrong with it.

(This is a code for a simple battleship game)

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)
print ship_row
print ship_col

playagain=True


def battleship(gr,gc):
  if gr==ship_row and gc==ship_col:
  print('you are correct')
  playagain=False
***elif board[gr][gc])=='X':***
  print('You guessed that one already')
  playagain=True
else:
  print('You are incorrect')
  playagain=True

while playagain=True:
  guess_row=input('Guess Row')
  guess_col=input('Guess Col')
  battleship(guess_row, guess_col)

Aucun commentaire:

Enregistrer un commentaire