11 Danger will Robinson


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

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
print "You missed my battleship!"
board[guess_row][guess_col] = "X" #2d indices syntax

my question is
why "board[guess_row][guess_col] = "X" " only has one "="
why is it not "board[guess_row][guess_col] == "X" ?"



board[guess_row][guess_col] = "X" is an affectation.
It means that now board[guess_row][guess_col] contains "X"....
but board[guess_row][guess_col] == "X" is a comparaison which returns true or false...


I think I sort of got what you are saying


When you use = you are assigning the value to the right of the = to be stored in the variable to the left of =. So, board[guess_row][guess_col] = "X" actually assigns "X" as the element in [guess_col] within the list [guess_row], which is inside another list variable called board. Whatever value used to be there is now gone and replaced by X.

When you use == you are comparing values on both sides of the == symbols to see if they're equal. So if you had board[guess_row][guess_col] == "X" the program would assume you're trying to compare the two; it would pull the value currently stored in board[guess_row][guess_col] and see if it's equal to the string X. Then it would return either True or False.


Gotta!!! Thank you :slight_smile: