Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8957323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:57:50+00:00 2026-06-15T14:57:50+00:00

Fairly simple question, I imagine…I have literally just installed Python and am testing some

  • 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")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:57:51+00:00Added an answer on June 15, 2026 at 2:57 pm

    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 ?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Fairly simple question: I have an init method on my class that has the
A fairly simple question for which I have a guess, but I can't find
I have a fairly simple question, however the answer seems to elude me. If
I have a fairly simple question about these 2 templating engines. I'm trying to
I have a fairly simple question: how to take one row of cv::Mat and
I have what I hope is a fairly simple question about using the value
I have a fairly simple question about natural/surrogate key usage in a well-defined context
I have a fairly simple question concerning NSString however it doesn't seem to do
I have a fairly simple question, but I can't seem to find a simple
I actually have a fairly simple question but I'm unable to find an answer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.