Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
I seem to be getting the same Error message when running the Binary Search Algorithm myself...not sure why?
def binary_search(list, target): first = 0 last = len(list) - 1 while first <= last: midpoint = (first + last)//2 if list[midpoint] == target: return midpoint elif list[midpoint] < target: first = midpoint + 1 else: last = midpoint - 1 return None def verify(index): if index is not None: print("Target found at index: ", index) else: print("Target not found in list") numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] result = binary_search(numbers, 12) verify(result) result = binary_search(numbers, 6) verify(result)
MOD: I updated your code so that it's formatted, and others can better help you. You can find out how to do this in the Markdown Cheatsheet that is right below the textbox when you're entering a question. Thanks!
Louise St. Germain19,408 Points
Hi! With the formatting in place it's much more obvious - everything from
while first <= last: all the way to (and including)
return None needs to be indented one more level. Otherwise, none of that is inside the binary_search method you defined.
Give it a try and see if it works!
Thanks for your help.
I've tried to indent...no success I'm afraid