Battleship - what is wrong with my code?


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-python/lessons/battleship/exercises/test-run?action=lesson_resume

<In what way does your code behave incorrectly? Include ALL error messages.>
File “python”, line 36
if board[guess_row][guess_col] = “X”
^
SyntaxError: invalid syntax

<What do you expect to happen instead?>
well i was hoping it would work!


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 = int(random_row(board))
ship_col = int(random_col(board))

guess_row = (raw_input("Guess Row: "))
guess_col = (raw_input("Guess Col: "))

if guess_row==ship_row and guess_col==ship_col:
    print "Congratulations! You sank my battleship"        
else:
    if  guess_row not in range(5) or \
        guess_col not in range(5):
           print "Oops, that\'s not even in the ocean."
    else:
        if (board[guess_row][guess_col] == "X"):
          print "You guessed that one already.You missed my battleship!"
  	else: 
   			if board[guess_row, guess_col] = "X"
    print_board(board)

#2

here:

if board[guess_row, guess_col] = "X"

you want to update the board, so why would you use if keyword?


#3

Ok so that line needs to be a new string and not an ‘else’?


#4

how do we update a variable?

like so:

variable = "new value"

you do the same thing here:

if board[guess_row, guess_col] = "X"

except you update an element in list (so not entire variable)

but in both cases, an if keyword should not be used


#5

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