When I run this function (by taking lower and upper bounds as 0 and len(list)-1), it works fine. But when the key is not there in the search_list I get a bad error. Any way to fix this so it says that the name/key was'nt found in the list?
def binary_search_recursive(search_list, key, lower_bound, upper_bound):
middle_pos = (lower_bound + upper_bound) // 2
if search_list[middle_pos] < key:
binary_search_recursive(search_list, key, middle_pos + 1, upper_bound)
elif search_list[middle_pos] > key:
binary_search_recursive(search_list, key, lower_bound, middle_pos - 1)
else:
print('Key is at Position', middle_pos)