有人能告诉我为什么这段代码无法使用吗?
list=[5,9,2,5,13]
item=input("Please enter the search item.")
found=false
for search = 0 to list.length -1
if item==list[search]:
found= True
next search
if found==True:
print("The item is in the list.")
else:
print("The item is not in the list.")
答案 0 :(得分:1)
你至少应该以更好的方式写作,比如:
lst=[5,9,2,5,13]
item = int(input("Please enter the search item."))
found = False
for search in range(len(lst)):
if item == (lst[search]):
found = True
if found:
print('{} is in the list'.format(item))
else:
print('{} is not in the list'.format(item))
答案 1 :(得分:0)
我不确定语法,如果确实是python。我建议你先查阅一下教程。
但是,您可以在列表中搜索元素:
list=[5,9,2,5,13]
element = raw_input("Please enter the desired element")
if int(element) in list: #note i've put int() because your list is made out of numbers rather than strings.
print element + " is in the list"
else:
print element + " is not in the list"
答案 2 :(得分:-1)
这就是你要求的:
value = int(input('Please enter a value to search: '))
if value in [5, 9, 2, 5, 13]:
print('Item found.')
else:
print('Item not found.')