Searching Tree: Binary Search
[deleted]

What is “Searching Tree”?

“Searching Tree” is a series of Python tutorials that teach you how to make a number searching algorithm that I made. I’m using Python because it’s popular and efficient.

What is binary search?

Binary search is a searching algorithm. It works by taking a number(say, 100) and splitting it in half over and over again until it gets to the correct number.

How to make it

Step 1: define the variables

You need three variables: currnum(so the computer knows what number it’s dealing with), correct(so the computer knows if it’s correct), and inputs(so the user can input if the number is correct or not). Your code should look like this:

#We are dealing with 50 first
currnum = 50
#We haven’t checked yet, so it’s false
correct = False
#We need an input and it needs to be lowercase
inputs = input(“Pick any number between 1 and 100.\nIs it more or less or equal to 50?\n”).lower()

Step 2: make the loop

Now we need to make the loop that loops until the number is correct. So it should look like this:

#We are dealing with 50 first
currnum = 50
#We haven’t checked yet, so it’s false
correct = False
#We need an input and it needs to be lowercase
inputs = input(“Pick any number between 1 and 100.\nIs it more or less or equal to 50?\n”).lower()
#The loop iterates until the number is correct
while correct == False:
   #...

Step 3: add the if

Now, we will add the if loop that checks if it’s correct. It should check for equal, less, and more.

#We are dealing with 50 first
currnum = 50
#We haven’t checked yet, so it’s false
correct = False
#We need an input and it needs to be lowercase
inputs = input(“Pick any number between 1 and 100.\nIs it more or less or equal to 50?\n”).lower()
#The loop iterates until the number is correct
while correct == False:
   if inputs == “equal”:
      #...
   elif input == “less”:
      #...
   elif input == “more:
      #...

Step 3: finishing touches

Now it should look like this:

#We are dealing with 50 first
currnum = 50
#We haven’t checked yet, so it’s false
correct = False
#We need an input and it needs to be lowercase
inputs = input(“Pick any number between 1 and 100.\nIs it more or less or equal to 50?\n”).lower()
#The loop iterates until the number is correct
while correct == False:
   if inputs == “equal”:
      print(”Yay! I guessed it!”)
      correct = True
   elif input == “less”:
      correct /= 2
      inputs = input(“Is it more or less or equal to” + str(correct) + “?\n”)
   elif input == “more:
      correct /= 2
      correct += correct
      inputs = input(“Is it more or less or equal to” + str(correct) + “?\n”)

Sorry if I didn’t explain well, I’m in a rush.

You are viewing a single comment. View All
Highwayman (1103)

@CodeABC123 uhI mean If you are doing all your writing, planning, editing, and reviewing all when your put it in the post immediately, then yes. It is hard.

BUT!

You don’t have to do that, you can just write, plan, edit, and review your tutorial in a text editor, much like you would your code in an IDE. :)