我的Python二进制搜索代码出了什么问题?

时间:2017-02-24 12:25:23

标签: python binary-search

我仍然不确定为什么这段代码不起作用,因为我试图纠正错误给我带来的每一个错误,但是我仍然无能为力。

import random
import math

numbas=[]

def randomnumbers():
    return random.randint(1, 9)

for i in range(10):
    numbas.append(randomnumbers())

def binarysearch(x):
    newlist=x
    newlist.sort()
    ettiqete=len(newlist)/2
    search=int(input("Search for a number: "))
    while(True):
        if type(newlist[ettiqete]) is float:
            betternet = newlist[ettiqete]
            if search == math.floor(newlist[ettiqete]):
                print("Wayyy.")
                break
            elif search > math.floor(newlist[ettiqete]):
                newlist=newlist[ettiqete-1:0]
            else:
                newlist=newlist[ettiqete+1:len(newlist)]

错误就是这个,我也发布了我采取的两个步骤。

binarysearch()
Search for a number: 7
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    binarysearch()
  File "C:/Users/djames1/Documents/Pithon/binarysearch.py", line 18, in binarysearch
    if type(newlist[ettiqete]) is float:
TypeError: list indices must be integers, not float

0 个答案:

没有答案
相关问题