What is meaning of the error and where is the mistake?
I got the TypeError: 'function' object is not subscriptable
for the following line:
row = random.choice(get_the_valid_locations[-2])
here is the get_the_valid_locations function:
def get_the_valid_locations(board):
valid_locations = []
for col in range(COLS_OF_BOARD):
for row in range(ROWS_OF_BOARD):
if available_square(board, row, col):
valid_locations.extend([row, col])
return valid_locations
get_the_valid_locations[-2]
is an int
(I checked).
If you need more Code just ask for it the comments!
The line that gives the error has a small mistake.
Now you try to take the second to last element of a function, but you I think you to give -2
as an argument to the function.
In that case the line should be:
row = random.choice(get_the_valid_locations(-2))
Then row
will contain a random element from the output of your function.
EDIT
A comment (credits: buran) noted that the function requires a 2d array, in that case I think you mean well, but implement it wrong. You want the output to be an array of colums and an array of rows. Your statement would be used to choose a random value of the rows returned by the functions.
You could do it like this:
def get_the_valid_locations(board):
valid_rows = []
valid_columns = []
for col in range(COLS_OF_BOARD):
for row in range(ROWS_OF_BOARD):
if available_square(board, row, col):
valid_rows.append(row)
valid_columns.append(col)
return [valid_rows, valid_columns]
usage:
randomRow = random.choice(get_the_valid_locations(board)[0])
or
randomColumn = random.choice(get_the_valid_locations(board)[1])
row = random.choice(get_the_valid_locations(board))