Q:

Python3--How to implement binary search in number game

Hello I am working on learning binary search trees. The program explains itself. I am confused on whether or not I should write a function to half the numbers. I am just confused on the logic of it all. If you could help I would greatly appreciate it.
list_of_numbers = [i for i in range(1,101)]
print("THIS IS YOUR LIST OF NUMBERS", list_of_numbers)

lowest_number = 1
highest_number = len(list_of_numbers)-1

mid = (len(list_of_numbers)) / 2
print("IS THIS YOUR NUMBER???:", mid)

number_of_guesses = 0

while lowest_number <= highest_number:
    print('Is your number: ', mid)
    print('please enter "<,>, or ="')
    first_guess = input("???:::::::::")
    first_answer = first_guess
    print(first_answer)
    equals = "="
    greaterthan = ">"
    lessthan = "<"    
    if first_answer == equals:
        print('Great JOB I GOT YOUR NUMBER ON THE FIRST TRY')
        break
    if first_answer == lessthan:
        print('The number is now between 49-1, half it.')
        break
    elif first_answer == greaterthan:
        print('The number is now between 51-100')
        break
    else:
        print('Type one of the answers')
        break

    number_of_guesses = number_of_guesses + 1

A:

You don't need a function to simply divide by 2. What you should be doing for a binary search is keeping track of the upper and lower bounds for the number; then you can calculate the guess as (upper + lower) / 2. If the player says the guess is too high, then set the upper bound to the current guess. If they say the guess is too low, then set the lower bound to the current guess. Repeat that until the correct number is found.
BTW, this isn't really a search tree, it's just a basic binary search. A search tree would be storing all the possible numbers in a connected tree graph and using that to search for the answer, which this isn't doing.

