如何修复“类型”不是可订阅的错误,如果对于python 3.6.3,如果target == list [middle]“?

时间:2017-12-18 01:24:52

标签: python-3.6

尝试对列表进行排序,然后确定用户输入是否在列表中,并且我在递归二进制搜索中不断收到错误。

def main():
    list = ['Paul', 'Aaron', 'Jacob', 'James', 'Bill', 'Sara', 'Cathy', 
'Barbara', 'Amy', 'Jill']
    print("Unsorted list:")
    print(list)
    bubbleSort(list)
    print("Sorted list:")
    print(list)
    binarySearch()

这是我对列表进行排序的地方

def bubbleSort(list): #sorting of the list
    firstUnsorted = 0
    swapped = True
    while firstUnsorted < len(list) and swapped:
        index = len(list)-1
        swapped = False
        while index > firstUnsorted:
            if list[index] < list[index-1]:
                swap(list, index-1, index)
                swapped = True
            index = index-1
        firstUnsorted = firstUnsorted + 1

def swap(list, index1, index2):
    temp = list[index1]
    list[index1] = list[index2]
    list[index2] = temp

这是递归二进制搜索的第二部分和我收到错误的部分

def recursiveBSearch(names, target, first, last): 
    if first > last:
        return False
    else:
        middle = int((first + last)/2)
        if target == list[middle]: #this is where I am getting on my error
            return True
        else:
            if target < list[middle]: #this is where I am getting the error
                recursiveBSearch(first, middle-1)
            else:
                recursiveBSearch(middle + 1, last)

这是递归二进制搜索的第一部分

def binarySearch(): #first part of recursive binary search
    names = [list]
    aName = input("enter a name to be found: ")
    first = 0
    last = len(names)-1
    if recursiveBSearch(names, aName, first, last):
        print(aName, "was found")
    else:
        print(aName, "was not found")

0 个答案:

没有答案