Board[0]


#1

My bad I accidentally sent the post, I figured out the way to solve it.

Anyways for anyone that's curious:

https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/0/8?curriculum_id=4f89dab3d788890003000096

In this lesson the random_col code changed from this:

def random_col(board):
    return randint(0, len(board) - 1)

to this:

def random_col(board):
    return randint(0, len(board[0]) - 1)

From my understanding board[0] would print out 5 'O's (the first row of board), and the usage of len() - 1 is to make sure the values change when the grid size changes.

So if the rows and columns do not form a perfect square (say it's 4 x 5 instead of 4 x 4), then the rows are limited to 4 and the columns limited to 5.

If we still use len(board) - 1 here, the value would return to be 4, so the integer will random between 0 and 4. If we use len(board[0]) - 1 instead, the value would return as 3, so the integer will random between 0 and 3.

Since there are only 4 rows now, len(board) - 1 will have a chance of randoming into a row that does not exist in the 4x5 grid. Therefore len(board[0]) - 1 will be a more accurate solution.


#2

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