Last answered:

14 Feb 2023

Posted on:

21 Apr 2022

0

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)
Instructor
Posted on:

21 Apr 2022

0

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

Posted on:

14 Feb 2023

0

I think no need to test the midpoint again if it's already been tested, as midpoint == item

Submit an answer