I'm a beginner programmer and I wanted to make my own binary search algorithm from scratch without any help. It's safe to say it's not going well. I was hoping someone could find the error in my code. I fixed a few issues, but now I've ran into the issue where if can't find values greater than the middle index, and it is not ending once it reaches a single value not equal to the target.
import random
userInput = 100
numbers = []
for i in range(100):
numbers.append(random.randint(0, 100))
#Adding 100 to use as a test
numbers.append(100)
numbers.sort()
print(numbers)
def binarySearch(list, target):
midIndex = int(len(list)/2)
midValue = list[midIndex]
if midValue == target:
print("We found " + str(target))
elif target > midValue:
newList = numbers[midIndex:]
binarySearch(newList, target)
elif target < midValue:
newList = numbers[:midIndex]
binarySearch(newList, target)
elif len(list) == 1 and list[0] != target:
print(target + " is not in the list")
else:
print("It's not in the list")
binarySearch(numbers, userInput)
lower_case_with_underscoresstyle.