【发布时间】:2020-10-29 07:35:53
【问题描述】:
n = int(input("Enter size of array"))
num = int(input("Enter numbers"))
for i in range(0, n):
num = int(input(hello.append(num)))
print(hello)
x = int(input("enter number to check!"))
count = 0
while count <= len(hello):
if (hello[count]==x):
print("found")
break
elif (hello[count]!=x):
count = count + 1
else:
print("not found")
break
朋友们好,我是 DSA 初学者,我的线性搜索代码中出现错误。如果我输入列表中存在的元素,代码将执行,但如果我输入不存在的元素,它会给我
"if (hello[count]==x): Index Error: list index out of range"
帮助我了解我的代码如何高效。 TNX
【问题讨论】:
-
索引从0开始,表示最后一个索引比列表长度小1。
标签: python algorithm index-error linear-search