Python 2.7 请告诉我怎么了??它只显示未找到的元素 def binsearch(a,c): 第一个=0 last=len(a)-1 发现=错误 第一个
错误是Python 2.7 请告诉我怎么了??它只显示未找到的元素 def binsearch(a,c): 第一个=0 last=len(a)-1 发现=错误 第一个,python-2.7,Python 2.7,错误是c是一个字符串。 只需将c=int(c)添加到c=raw\u输入(“…”)行之后即可非常感谢您的帮助……它成功了 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
c
是一个字符串。
只需将
c=int(c)
添加到c=raw\u输入(“…”)
行之后即可非常感谢您的帮助……它成功了
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."