I am coming from C# background and I have hard time figuring out how to run python script.
So, I wrote this simple recursive binary search and found online that I can do something like this:
def chop(array, search, lo, high):
if lo <= high:
middle = (high + lo) /2
if array[middle] == search:
return 'true'
elif search > array[middle]:
return chop(array, search, middle + 1, high)
else:
return chop(array, search, lo, middle -1)
return 'false'
if __name__ == '__main__':
a = [1,2,3,4,5,6,7,8,9,10]
print chop(a, 21, 0, len(a) -1)
the __main__ will be main my method to call the chop function from but it doesn’t work. I have it saved in a test.py file. Also I though I can somehow run just the chop function from Python Shell, but I have no idea how to do it. Please advice. Thank you.
If you are in the directory where the script is located, just run
If you want to run the chop function from the interpreter, start the interpreter in the directory where the script is located and execute
That should do it. If it doesn’t, you probably have syntax or other errors in your code, like indentation that you already discussed in comments to your question.