我有一個產生錯誤的Python 3.x的程序:類型錯誤:列表索引必須是整數,而不是浮
def main():
names = ['Ava Fischer', 'Bob White', 'Chris Rich', 'Danielle Porter',
'Gordon Pike', 'Hannah Beauregard', 'Matt Hoyle',
'Ross Harrison', 'Sasha Ricci', 'Xavier Adams']
entered = input('Enter the name of whom you would you like to search for:')
binary_search(names, entered)
if position == -1:
print("Sorry the name entered is not part of the list.")
else:
print(entered, " is part of the list and is number ", position, " on the list.")
input('Press<enter>')
def binary_search(names, entered):
first = 0
last = len(names) - 1
position = -1
found = False
while not found and first <= last:
middle = (first + last)/2
if names[middle] == entered:
found = True
position = middle
elif names[middle] > entered:
last = middle - 1
else:
first = middle + 1
return position
main()
錯誤是:
TypeError: list indices must be integers, not float
我無法理解什麼這個錯誤信息的意思。
請給予包含回溯的完整錯誤消息。 – BrenBarn