def find_val():
upper = 100
lower = 0
found = False
while not found:
i = upper - lower / 2
val = f(i)
if desired_val - tolerance < val < desired_val + tolerance:
found = True
elif val > desired_val:
lower = i
else:
upper = i
Keep a little list of the values at the critical points and choose the one that makes the biggest/bestest value, and don't forget the ends of the range