0

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!

9
  • you're using subscription operator instead of using function calling Commented Jan 9, 2021 at 13:27
  • how should I do it instead? Commented Jan 9, 2021 at 13:28
  • 1
    it's unclear what -2 is supposed to be. Presumably you want element at index -2 from the list returned by get_the_valid_locations. However you need to call that function and pass argument for board (2D list?). We don't know what object/variable you want to pass for board. Commented Jan 9, 2021 at 13:34
  • please, post minimal reproducible example Commented Jan 9, 2021 at 13:37
  • @buran you're right that's why I deleted my answer as it is not useful and may mislead. I wonder from where COLS_OF_BOARD and ROWS_OF_BOARD is coming from. Probably global variables? Commented Jan 9, 2021 at 13:42

3 Answers 3

1

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])

Note: if you just want a random position on the `board` object, your code is correct, but your call should be:
row = random.choice(get_the_valid_locations(board))
Sign up to request clarification or add additional context in comments.

3 Comments

get_the_valid_locations expects argument for board, most likely 2d list. calling it with board=-2 will cause error.
Yes I think buran is right. Is there a possibility I can put both (board) and (-2) inside?
I made some edits, if they don't cover your problem, try explaining it a bit more
0

assuming board is a name that you want to pass as argument for parameter board and you want just last 2 elements of the list returned from the function:

row = random.choice(get_the_valid_locations(board)[-2:])

This will randomly select one value out 0f the 2 last values.

Comments

0

In my case, this error shows when you accidentally (e.g. made a typo) use brackets instead of parentheses to call your function, e.g.:

def my_func(arg1):
   pass
my_func[arg1] # oops

Solution: simply call the function with parentheses instead:

def my_func(arg1):
   pass
my_func(arg1) # fixed

So similarly, in your case, you should simply change the brackets into parentheses to call your function:

Broken:

get_the_valid_locations[-2] # X

Fixed:

get_the_valid_locations(-2) # O

Just make sure your function is actually expecting an integer. It's unclear what type board is, so I highly recommend that you start using type hints in your function signatures.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.