Jul-23-2024, 02:01 PM
(Jul-23-2024, 01:07 PM)deanhystad Wrote: Indenting in Python is syntax, not just a way to make the code look pretty. Indenting defines code blocks. In your version of binary_search the function returns None if the first guess is incorrect. This happens because the indenting for "return None" places it inside the block of code executing in the while loop. In the version posted above indenting places "return None" outside the while loop. "return None" does not execute until the "while low <= high:" loop has completed. Four spaces making all the difference here.Oh man... I feel like it's really a stupid mistake.
You make the same error in find_smallest(). Because of the indent error you return the first element instead of the smallest element. When you've removed all but the last element the for loop doesn't execute and the function returns None (default value for functions that don't execute a "return" statement). You get the error message when this happens.
Now the codes is working fine.
1 [Finished in 64ms]
[3] [Finished in 63ms]Thank you very much man, now I've learn what indent is.