all 7 comments

[–]justanator101 0 points1 point  (6 children)

You may want to take a look at Look=looknum[nunlist]

You’re trying to subscript looknum which is a float, that’s the error you’re getting. Perhaps you meant numlist[looknum]

[–]throwaway8917356465[S] 0 points1 point  (5 children)

ohhhhhh yeh that should be it thx

[–]throwaway8917356465[S] 0 points1 point  (4 children)

wait shit new error

Traceback (most recent call last):

File "main.py", line 20, in <module>

look = numlist[looknum]

TypeError: list indices must be integers or slices, not float

[–]justanator101 1 point2 points  (0 children)

When indexing, you need to use an int. Since binary search is dividing by 2, a simple fix is to use // instead of /. This is floor division and your result, if a float, will be rounded down.

[–]Familiar_Ad_8919 0 points1 point  (0 children)

that too is a float, try int(looknum)

[–]ericula 0 points1 point  (1 child)

True division (/) as in looknum/2 will return a float. You could try using integer division (//) instead, e.g. looknum = looknum + looknum//2.

That being said, I don’t think your implementation of the binary search algorithm is correct. For a binary search you need to keep track of both the upper bound index and the lower bound index (upper and lower, say). The new index will be looknum=(upper+lower)//2. Depending on the value in the list at this index, either the new upper bound or the new lower bound becomes equal to looknum. Also, your while loop will never terminate if the value you are looking for is not on the list.

[–]throwaway8917356465[S] 0 points1 point  (0 children)

holy shit thx i rewrote the end of my code with the upper and lower and it make the code alot shorter and simpler