11. Danger, Will Robinson!


I get stucked. Сode looks like correct, but it doesn't change "O" to "X". Help, please.

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)


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!"
    print "You missed my battleship!"
    print board[guess_row][guess_col] == 'X'


Well you've identified the issue, so go look at the part that is responsible for changing to "X" and consider what that is supposed to look like.

If you really think it should look that way, then I suggest you do a little experimenting at the bottom of your code where you print the board, try to set some location to X, and then print it again to see if you succeeded.


You have to change comparison element which is double =. What I'm trying to say that if you change print board[guess_row][guess_col] == 'X' with print board[guess_row][guess_col] = 'X' it will work


Yes, I tried this. But it doesn't work too.
File "python", line 34
print board[guess_row][guess_col] = 'X'
SyntaxError: invalid syntax


Попробуй вместо апострофа кавычки тогда


try to change
print "You missed my battleship!"
print board[guessrow][guess_col] == 'X'_

to following

print "You missed my battleship!"
board[guess_row][guess_col] = "X"


Tried this too. Same output :frowning:


that's weird 'cause it works for me


Weird, but true


This is what worked for me
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
I think it is just the print in front of board[guess_row][guess_col] = "X" making it not work


Yes, you're right. Thanks