Reputation: 205
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?
Upvotes: 4
Views: 56673
Reputation: 1264
It took me couple of minute to figure out what is the mistake. Sometimes a blindspot stops you from looking at obvious.
Incorrect
msg = "".join['Temperature out of range. Range is between', str(
HeatedRefrigeratedShippingContainer.MIN_CELSIUS), " and ", str(
RefrigeratorShippingContainer.MAX_CELSIUS)]
Correct
msg = "".join(['Temperature out of range. Range is between', str(
HeatedRefrigeratedShippingContainer.MIN_CELSIUS), " and ", str(
RefrigeratorShippingContainer.MAX_CELSIUS)])
As you can see join is a method and has to be called with () which was missing and causing the issue. Hope it helps all to look for the method and add ().
Upvotes: 1
Reputation: 1122222
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.
Upvotes: 5
Reputation:
Python uses (...)
to call a function and [...]
to index a collection. Furthermore, what you are trying to do now is index the built-in function len
.
To fix the problem, use parenthesis instead of square brackets:
low, high = 0, len(li)
Upvotes: 2
Reputation: 8685
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 ()
Upvotes: 11