Fairly simple question, I imagine…I have literally just installed Python and am testing some of the beginner tutorials.
I wanted to create a menu that allows you to add items to a list, then check to see if theyre added: testing functions, and lists in the process.
#create empty list and define variables
firstlist = {'joe'}
additem = "test"
printthis = "test"
#create menu, add or check name
def menu():
#print what options you have
print "Add to list: type '1'"
print "Check for name: type '2'"
print "To exit program: type '3'"
return input ("Choose your option: ")
def addmenu():
additem = input("Name of list item: ")
firstlist.append(additem)
print additem, "has been appended"
def checkmenu():
printthis = input ("What are you looking for?: ")
if firstlist.has_key(printthis):
print "is in the list"
else:
print "is not in the list"
# Perform action
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
addmenu()
elif choice == 2:
checkmenu()
elif choice == 3:
loop = 0
elif choice > 3:
print "You made an incorrect selection"
Heres my error:
Traceback (most recent call last):
File "C:\Python27\testing python\tutorials\dictionaryselection", line 32, in <module>
addmenu()
File "C:\Python27\testing python\tutorials\dictionaryselection", line 15, in addmenu
additem = input("Name of list item: ")
File "<string>", line 1, in <module>
NameError: name 'TESTING' is not defined
Not sure whats going on…Any help would be appreciated.
Working code below: converted to python 3.x
#create empty list and define variables
firstlist = ['Joe']
additem = "test"
printthis = "test"
#create menu, add or check name
def menu():
#print what options you have
print ("")
print ("Add to list: type '1'")
print ("Check for name: type '2'")
print ("To list the whole list '3'")
print ("To exit program: type '4'")
print ("-------------------------")
return input ("Choose your option: ")
def addmenu():
additem = input("Name of list item: ")
firstlist.append(additem)
print (additem, "has been appended")
def checkmenu():
printthis = input("What are you looking for?: ")
if printthis in firstlist:
print ("is in the list")
else:
print ("is not in the list")
def listlist():
print (firstlist[1])
# Perform action
loop = 1
choice = 0
while loop == 1:
choice = int(menu())
if choice == 1:
addmenu()
elif choice == 2:
checkmenu()
elif choice == 3:
listlist()
elif choice == 4:
loop = 0
elif (choice > 4):
print ("You made an incoorect selection")
There are multiple errors in the example, let us go through them. First, if you want a list then you need to define it as such, i.e.:
Now, since you are using Python 2, it is always recommended to use
raw_inputin place ofinput. The later will applyevalon the obtained string, so it will evaluate the input as Python code. You generally don’t want that for security reasons (I know this is an example). So, let’s change everyinputtoraw_input. The problem now is that entering a string that represents a number while usingeval, actually converts the string to a number. Now you need to do the same, but usingraw_input. Since your options are limited to integer values, the solution isint(raw_input()).The third problem is related to
has_key. If the object used is asetor alist, then there is no such methodhas_keydefined for them. That would work if the object in question were adict, but it is not. The proper way to check for containment in the given code issomething in A. Doing this while usingsetis much more efficient than while usinglist, and the code remains the same (except you need to changeappendtoadd).Can you adjust your code now ?