11. Danger, Will Robinson


#1


https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/1/2?curriculum_id=4f89dab3d788890003000096#

I keep getting this error message (below) when I enter col and row that are within the board....

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


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(print_board(board))


#2

Hi @ttdd1208,

The actual problem is here, rather than with what you entered when you ran the program ...

    guess_row = "X"
    guess_col = "X"

Instead of the above two statements, use guess_row and guess_col as indexes to set the appropriate cell in board to "X". That cell can be accessed as board[guess_row][guess_col].


#3

Done that, Its still not working


#4

We'll need to see exactly what changes you made. Please post all of your code for this exercise again and any error messages you received.


#5

I am confused about the indexes, could you explain how that works? Thanks for your help, it is working I just want to make sure I fully understand.


#6

board is a list of five lists. Each of the five lists represents a row with five cells, with each cell being part of one of the five columns. We can also think of board as a two-dimensional list. In any case, we need two indexes to specify a location within board. The first index represents the row and the second represents the column. Accordingly, to place an "X" in the guessed location, we do this ...

    board[guess_row][guess_col] = "X"

#7

2 posts were split to a new topic: Danger, Will Robinson! - Row and Column Numbers


#8

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