num_array = list( )
num =int(input("Enter how many elements you want:"))
print ('Enter numbers in array: ')
for i in range(int(num)):
n = input("num :")
num_array.append(int(n))
print ('LIST: ',num_array)
key=int(input ('enter element to be searched:'))
if key==num_array[i]:
print('element found')
else:
print("not found")
所有关于python中的简单线性搜索我使用了C的伪代码并实现了它 enter image description here
答案 0 :(得分:0)
基本上,你只是忘了在检查时循环遍历循环。
key=int(input ('enter element to be searched:'))
found = False;
for num in num_array:
if key==num:
print('element found')
found = True;
if(found==False)
print("not found")