Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'builtin_function_or_method' object is not subscriptable

def binary_search(li, targetValue):
    low, high = 0, len[li] #error on this line
    while low <= high:
        mid = (high - low)/2
        if li[mid] == targetValue:
             return "we found it!"
        elif li[mid] > targetValue:
             low = mid - 1;
        elif li[mid] < targetValue:
             high = mid + 1;
    print "search failure "

just posted this question recently, but my code still doesn't work?

like image 401
user2928929 Avatar asked Nov 14 '13 22:11

user2928929


People also ask

What is Builtin_function_or_method?

This usually happens when a function or any operation is applied against an incorrect object. In such a situation, you are likely to encounter an error called typeerror 'builtin_function_or_method' object is not subscriptable.

What is Subscriptable object in Python?

In Python, the subscripting is nothing but indexing since it is the same as a mathematical notation that uses actual subscripts. In Python, for example, we use array[0] to get the first element of the array and the mathematicians use a0 for the same thing.

How do you make an int object Subscriptable?

The “typeerror: 'int' object is not subscriptable” error is raised when you try to access an integer as if it were a subscriptable object, like a list or a dictionary. To solve this problem, make sure that you do not use slicing or indexing to access values in an integer.


2 Answers

you are using the wrong brackets len(li) not len[li]

Remember when you are trying to access a function you need to use function(args) if you use [] you are actually accessing a sequence like a list. your_list[index]. len is a built in function so you need the ()

like image 56
jramirez Avatar answered Nov 15 '22 10:11

jramirez


len is a built-in function, but you are trying to use it as a sequence:

len[li]

Call the function instead:

len(li)

Note the shape change there, indexing is done with square brackets, calling is done with round parentheses.

like image 29
Martijn Pieters Avatar answered Nov 15 '22 10:11

Martijn Pieters