11. Danger, Will Robinson!


I don't know what a list element is and i do't understand how to change it or set it.Help

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!"
else :
    print "You missed my battleship!"
    guess_row = 
    guess_col = 


list = [ ], this is empty list
list2 = ['name', 'someone', 'something', 'string'], this is list with elements in it

first element in list is at index 0, second at index 1 and so on

to change element in list you need to access element you want to change

del list2[2], this will delete third element


So i guess i'll have to write guess_row["O"]["X']?


no, you need to access elements in your list, in this case board


So below the range loop i simply replace "O" with "X"?


board[guess_row][guess_col] = 'X'

do you understand the concept?


Nope.But i will try to.Thanks.If possible explain a bit


It means that in a one dimensional list such as board = [O, O, O, O, O]`, you only need to specify one index in order to access the element in that index. For example board[1] will let you access the second element on that list and you can modify it however you want just like board[1] = 'X' will output : [O, X, O, O, O]. Nevermind the absence of quotes, just assume we used the .join() function already

With that in mind, I hope that gave you an idea on how board[guess_row][guess_col] = 'X' modify the element on that two-dimensional list. We specify two indices because the list is two dimensional.


Don't forget the;

print print_board(board)