Danger, Will Robinson!


#1


Hi All,

This is driving me crazy, I've executed the exercise however I get the below error

Oops, try again.
Make sure to enter a col and row that is on the board!


Not really sure where what is going wrong, please check and advise.


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 = 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:
    guess_row = "X"
    guess_col = "X"
    print "You missed my battleship!"
    
print_board("X")

I get the result as below

O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row: 6
Guess Col: 6
2
4
You missed my battleship!
X
None

Not sure what's wrong, please advise.


#2

these lines:

    guess_row = "X"
    guess_col = "X"

are wrong. Your board is a multi-dimensional list. so you should use board[index1][index2] to set the coordinate on the board to X (you can think what index1 and index2 should be)

then this line:

print_board("X")

you call the print_board function, which is a function to print the board, but then i would pass board as argument

And, when prompted, enter valid coordinates (you have a 5x5 board, how can 6 be on the board?) and remember that lists are zero indexed based


#3

alright so what exactly does this do?

board[index1][index2]?

I've corrected and it has passed now

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

print_board(board)

But didn't understand what I did if u could advise @stetim94


#4

but you did correct the things correctly? Which indicate you must understand a bit.

board is a multi-dimensional list. You created it here:

board = []

for x in range(0, 5):
    board.append(["O"] * 5)

now, you can use board[x][y] to access values from the board, or set a new value, which is what you do here:

board[guess_row][guess_col] = "X"

Then you call print_board (a function you wrote to display the board to the user) and pass board as argument, since you just updated board


#5

thank you, was able to understand