请问我有什么问题?它只显示未找到的元素

时间:2016-12-06 19:44:57

标签: python-2.7

def binsearch(a,c):

    first=0
    last=len(a)-1
    found=False

    while first<=last and not found:
        mid=(first+last)//2
        if(a[mid]==c):
            found=True           
        elif a[mid]<c:
             last=mid-1
        else:
            first=mid+1
    return found        
a=list()

n=raw_input("Enter how many elements:")

for i in range(int(n)):

    num=raw_input("Enter the elements:")
    a.append(int(num))


c=raw_input("Enter the element u wanna search:")

b=binsearch(a,c)

if b:

    print "Element",c,"found in position."

else:

    print "Element not found."

1 个答案:

答案 0 :(得分:1)

错误是c是一个字符串。 只需在c = int(c)

之后添加c = raw_input("...")即可