0

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")
3
  • 1
    Where did you get this example from ? It is wrong in multiple ways. firstlist could be a set or a list, but the definition would be wrong for the later and doesn't match the name. Given the formatting of print statements, you are using Python 2, thus you must generally use raw_input instead of input and convert the result as appropriated. input() is the same as eval(raw_input()), and that is the reason you get such an error.
    – mmgp
    Dec 7, 2012 at 16:14
  • i made the code up based on other examples. just helps me figure out how it works. I dont know what a set is...haven't gotten there yet! I believe I am using python 2. eval(raw_input()) did not seem to get rid of the error. Dec 7, 2012 at 16:16
  • 1
    @RickyMason He wasn't saying you should use eval(raw_input()), he was saying that's the equivalent of what you are already doing. Just use raw_input() since you're using Python 2.x. Dec 7, 2012 at 16:22

4 Answers 4

1

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.:

l = ['joe'] # this is a list
s = {'joe'} # this is a set

Now, since you are using Python 2, it is always recommended to use raw_input in place of input. The later will apply eval on 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 every input to raw_input. The problem now is that entering a string that represents a number while using eval, actually converts the string to a number. Now you need to do the same, but using raw_input. Since your options are limited to integer values, the solution is int(raw_input()).

The third problem is related to has_key. If the object used is a set or a list, then there is no such method has_key defined for them. That would work if the object in question were a dict, but it is not. The proper way to check for containment in the given code is something in A. Doing this while using set is much more efficient than while using list, and the code remains the same (except you need to change append to add).

Can you adjust your code now ?

2
  • thank you. your earlier comment finally hit me as I was looking through the original tutorial. This helped a lot. Dec 7, 2012 at 16:33
  • That is nice, Python's tutorial and its documentation in general is a good place to go.
    – mmgp
    Dec 7, 2012 at 16:38
1

With firstlist, you're mixing the idea of a list and the idea of a dictionary. It looks like you just want a list ...

firstlist = ['Joe']

also, instead of using has_key, write

if printthis in firstlist:
1
#create empty list and define variables
firstlist = {}
additem = ""
printthis = ""
removeitem = ""
#create menu, add, remove or check name
def menu():
    print "Add to list: type '1'"
    print "Remove from the list: type '2'"
    print "Check for name: type '3'"
    print "To exit program: type '4'"
    return input("Choose your option: ")
def addmenu():
    additem = raw_input("Name of list item: ")
    firstlist[additem] = additem
    print additem, "has been appended"
def removemenu():
    removeitem = raw_input("Name of list item: ")
    firstlist.pop(removeitem)
    print removeitem, " has been removed"
def checkmenu():
    printthis = raw_input("What are you looking for?: ")
    if firstlist.has_key(printthis):
        print printthis, " is in the list"
    else:
        print printthis, " is not in the list"

# Perform action
loop = 1
choice = 0
while loop == 1:
    choice = menu()
    if choice == 1:
        addmenu()
    elif choice == 2:
        removemenu()
    elif choice == 3:
        checkmenu()
    elif choice == 4:
        loop = 0
    elif choice > 4:
        print "You made an incorrect selection"
0

@ Ricky Mason : I have modified the code a bit. Here you would need a dict object and not a set or a list. A dict object #since has a key value pair would be easy to retrieve the values that you are looking for in the checkmenu and at the same #time you can remove the values from the list too. I hope this helps. Feel free to post any further queries regarding the same.

Here is the code in following reply

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.