【发布时间】:2017-09-04 07:05:39
【问题描述】:
我是 python 新手,我们被分配创建一个不使用“in”或索引的线性搜索程序。该程序编译但说我输入的每个数字都不在列表中。我也必须为二进制搜索做同样的事情,但我一次只做一件事情哈。任何帮助表示赞赏!
PS:如果不使用“索引”功能,如何显示它所在的索引?
def linearSearch(intList,target):
found = False
position = 0
while position < len(intList) and not found:
if intList[position] == target:
found = True
position = position + 1
return found
linearList = [3,5,9,7,6,12,15,9,1]
numInput = input("What number are you looking for? ")
numFound = linearSearch(linearList, numInput)
if numFound:
print("The number is in index: ")
else:
print("The number is not in the list")
【问题讨论】:
-
这里有一个提示来解决你的问题:输入是字符串类型,你正在将它与整数进行比较
标签: python function binary-search linear-search