why in the code of binary search use two if rather than elif
in the following code
if my_list[midpoint] == item:
found = True
else:
if my_list[midpoint] < item:
first = midpoint + 1
else:
last = midpoint - 1
why use two if?
cant I just use if ,elif , else?
2 answers ( 0 marked as helpful)
Hey,
Thank you for your question!
Yes, you could definitely implement it in the way you propose, namely:
if my_list[midpoint] == item:
found = True
elif my_list[midpoint] < item:
first = midpoint + 1
else:
last = midpoint - 1
Kind regards,
365 Hristina
I think no need to test the midpoint again if it's already been tested, as midpoint == item