我是python的新手,我们被赋予创建一个不使用“in”或索引的线性搜索程序的任务。该程序编译但是说我输入的每个数字都不在列表中。我还必须为二分查找做同样的事情,但我一次只做一件事情。任何帮助表示赞赏!线性搜索Python
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(numInput, linearList)
if numFound:
print("The number is in index: ")
else:
print("The number is not in the list")
这是一个提示,以解决您的问题:输入是字符串类型,并且您将其与整数进行比较 – karthikr