You are Here: Home

# what is wrong with my binary search code in binary

Announcements Posted on
TSR's new app is coming! Sign up here to try it first >> 17-10-2016
1. the code is quite complete but im running this error:
"Traceback (most recent call last):
File "G:/shsb/Computer science/Binary search.py", line 29, in <module>
if alpha[midpoint] < find:
TypeError: list indices must be integers or slices, not float"

this is the code that im running
alpha = ["a","b","c","d","e","f","g","h", "i","j"]
find = "b"
midpoint = int(len(alpha)//2)
lowerBound = 0
upperBound = len(alpha)-1
foundValue = False

while foundValue == False:
if find == alpha[midpoint]:
foundValue = True
print ("item found at position " ,midpoint)
break
if find == alpha[upperBound]:
foundValue = True
print ("item found at position " ,upperBound)
break
if find == alpha[lowerBound]:
foundValue = True
print ("item found at position " ,lowerBound)
break
else:
while alpha [midpoint] != find:
while alpha[lowerBound] != find:
while alpha[upperBound]!= find:
if alpha[midpoint] < find:
upperBound = midpoint
midpoint = (upperBound-lowerBound)/2
else:
lowerBound = midpoint
midpoint = ((upperBound-lowerBound)/2)+lowerBound

if any one code help it would be much appreciated
2. Sorry you've not had any responses about this. Are you sure you've posted in the right place? Here's a link to our subject forum which should help get you more responses if you post there.

You can also find the Exam Thread list for A-levels here and GCSE here.

Just quoting in Puddles the Monkey so she can move the thread if needed
Spoiler:
Show
(Original post by Puddles the Monkey)
x

## Register

Thanks for posting! You just need to create an account in order to submit the post
1. this can't be left blank
2. this can't be left blank
3. this can't be left blank

6 characters or longer with both numbers and letters is safer

4. this can't be left empty
1. Oops, you need to agree to our Ts&Cs to register

Updated: April 22, 2016
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Today on TSR

### How does exam reform affect you?

From GCSE to A level, it's all changing

Poll
Useful resources