def search_linear(x,y):
n = len( x )
for i in range(n):
if theValue[i] == y:
return True
return false
def main():
mainValues =int( input("enter the nos first"))
mV = mainValues.list()
trgt =int( input('enter a single number to be found in the list'))
def search_linear(mainValues, trgt)
这是我写的一个简单的线性搜索程序。运行时,它会在第17行显示invalid syntax
,指向trgt = ...
。
我不明白我哪里错了。
我编写搜索功能,并在main函数中分配值。这么简单的程序
答案 0 :(得分:3)
调用函数时不需要def
。只需使用search_linear(mainValues, trgt)
。
def
。
答案 1 :(得分:0)
#Tried my hand at a code by using simple keywords, no functions etc. Ideal for beginners.
ls=[]
n=input("Enter the number of elements in the list:")
k=n-1
for i in range(0,n,1):
b=raw_input("Enter the elements:")
ls.append(b)
print ls
element_to_be_searched=raw_input("Enter the element to be searched:")
for j in range(0,k,1):
if ls[j]==element_to_be_searched:
if j+1==1:
print "Search is successful and requested element is the 1st element"
break
elif j+1==2:
print "Search is successful and requested element is the 2nd element"
break
elif j+1==3:
print "Search is successful and requested element is the 3rd element"
break
elif j+1>3:
print "Search is successful and requested element is the ",j+1,"th element"
break
else:
print "Sorry,could not find the item you are looking for!"
希望它有所帮助!!!
答案 2 :(得分:0)
usernames = ['AB','CD','EF','GH','IJ','KL']
passwords = ['12','23','56','78','98','76']
def search (x,y):
n = len(x)
for i in range(n):
if usernames[i] == y:
print("It is here!")
return True
else:
print ("Incorrect!")
mainValues = input("Enter the username")
search(usernames,mainValues)
答案 3 :(得分:-1)
def LinearSearch(array, targetNumber):
for i in range (0,5):
array[i] == targetNumber:
print("The number %d does exist in the list." %(targetNumber))
array[i] != targetNumber:
print("The number %d does not exist in the list." %(targetNumber))
import random
random_number = []
for l in range (0,5):
random_number.append(random.randint(1,100))
# Algorithm Sort
for i in range(0, 5):
for j in range (5-1, -1, -1):
if random_number[j] < random_number[j-1]:
new_value = random_number[j-1]
random_number[j-1] = random_number[j]
random_number[j] = new_value
if j == 1:
break