线性搜索中的NameError

时间:2017-08-21 03:03:24

标签: python list data-structures linear-search

我应该用Python编写一个列表中的linearSearch脚本(找到一个项目)。我在Jupyter Notebook中做了代码,但是我得到了下面的错误,不知道为什么我得到这个错误。谁能告诉我如何解决错误?

以下是代码:

def linearsearch(myitem,mylist):
    Found = False
    position= 0
    while position < len(mylist) and not Found:
        if mylist[position] == myitem:
            Found = True
    position = position + 1
    return Found

if __name__ == "__main__":
    shoppinglist=["Apple","Orange","Banana"]
    item = input("what item are u looking for?")
    isitfound = linearsearch(item,shoppinglist)
    if isitfound:
        print("item is in the list")
    else:
        print("the item is not in the list")

当我运行代码时,我得到了“你在寻找什么项目?”。但是当我在列表中输入项目时,例如Apple我得到以下错误:

    163             self._sys_eval_input = builtin_mod.input
    164             builtin_mod.raw_input = self.raw_input
--> 165             builtin_mod.input = lambda prompt='': eval(self.raw_input(prompt))
    166         self._save_getpass = getpass.getpass
    167         getpass.getpass = self.getpass

NameError: name 'Apple' is not defined

0 个答案:

没有答案