Danger, Will Robinson!


Danger, Will Robinson!!

i need help
how do I set the list element at guess_row, guess_col to "X"

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!"


a = "Hello"
print a
#console >> Hello

a = "Bye"
print a
#console >> Bye

You can re-assign a variable's value as easy as that. There's no trick. You already have guess_row and guess_col, then it's just setting its values to the ones you want, most likely if something specific happens...


I don't understand
could you say it in a different way


Sure. If you printed guess_row and guess_col, it would show two numbers, the intersection that was your guess, right?
Then the exercise cleverly wants to know if your guess was a hit or a miss. So the question is, how do you express an intersection in a 2D list? So you can assign it to the "X" string when it's a miss.
The hint is actually very helpful:

To specify a single element in a 2-dimensional list, you need to give two indices. The syntax is: list_name[i][j], where i is an element in the outer list and j is an element in the inner list.

Hint: Your 2D list would be the board.


So [i] and [j] would be the respective guesses?


Yes, being careful with which one is which, right? You can always try and test it though. I know I messed up the first time doing that exercise.


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