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”:
shopping = [“apples”,“bananas”,“chocolate”,“pasta”]
item = input("what item do you wanna find? ")
isitFound = linearSearch(item, shopping)
if isitFound:
print (“Your item is in the list”)
else:
print (“item not in list”)
This is due to your use of the ‘input’ built-in function. It attempts to evaluate the string provided as a Python expression. It’s not for general user-input.
Python knows the purposes of certain names (ex. built-in functions ). Other names are defined within the program (ex. variables). If Python encounters a name that it doesn’t recognize, you’ll probably get NameError: global name ‘xx’ is not defined error. In most cases, this error is triggered when Python sees a variable name (Global or Local) and doesn’t know what it’s for. These errors can happen if you forget to initialize a variable , if you misspell a variable, or if you misspell a reserved word such as “True”. Before you use the global variable in your function for reading, it must be first initialized somewhere: either outside of the function or inside it.