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?
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.
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.
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.
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 ()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With