Danger, will robinson


#1



I think I have done this exercise correctly, however the following error message keeps appearing after I enter data on the console:


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


Can someone explain what I'm doing wrong?


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


#2

It's suggesting that the user input (which you haven't said what it was) isn't valid


#3

Is there a specific user input?


#4

wait hold on it did just let me guess a load of times and then I guessed right


#5

I suspect anything that's ON the board would do it, as opposed to outside it - for which there isn't any code yet


#6

You have to change the location that you have guessed with "X".So you have to make appropriate changes in the Board List ...so use board[guess_row][guess_col] = "X" so whatever location you guessed it will print "X" over there


#7

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