Home/Support/Support Forum/The code is giving an error NameError: name 'apples' is not define
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

The code is giving an error NameError: name 'apples' is not define

0 votes
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")
asked Mar 9, 2016 in Python by mikky New to the Community (0 points)

Please log in or register to answer this question.

2 Answers

0 votes
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.

You probably want 'raw_input' instead.
answered Mar 15, 2016 by DigiGuy42 Community Contributor (102 points)
0 votes
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.

http://net-informations.com/python/iq/global.htm
answered Mar 19 by germyrinn New to the Community (2 points)
...