arr=[1,2,3,4,5,6,7,8,9,10] head,tail=0,len(arr) target=int(input("Enter the target number:")) while tail-head!=1: mid=(tail+head)//2 if arr[mid]==target: ans=mid break if arr[mid]<target: head=mid+1 else: tail=mid else: if target==arr[(head+tail)//2]: ans=(tail+head)//2 else: ans=-1 print(ans)
Enter the target number:10
9