Python - 简单的列表函数,给出了名称错误

时间:2012-12-07 16:08:13

标签: python

相当简单的问题,我想......我刚刚安装了Python并正在测试一些初学者教程。

我想创建一个菜单,允许您将项目添加到列表中,然后检查它们是否已添加:测试功能和过程中的列表。

#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"

继承我的错误:

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

不确定最新情况......任何帮助都会受到赞赏。

下面的工作代码:转换为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")

4 个答案:

答案 0 :(得分:1)

示例中有多个错误,让我们一起来看看。首先,如果你想要一个列表,那么你需要定义它,即:

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

现在,由于您使用的是Python 2,因此始终建议使用raw_input代替input。后者将对获取的字符串应用eval,因此它将输入评估为Python代码。出于安全原因,您通常不希望这样(我知道这是一个例子)。所以,让我们将input更改为raw_input。现在的问题是在使用eval时输入表示数字的字符串,实际上将字符串转换为数字。现在您需要执行相同操作,但使用raw_input。由于您的选项仅限于整数值,因此解决方案为int(raw_input())

第三个问题与has_key有关。如果使用的对象是setlist,则没有为其定义此类方法has_key。如果有问题的对象是dict,那将会有效,但事实并非如此。在给定代码中检查包含的正确方法是something in A。使用set时执行此操作比使用list时效率更高,代码保持不变(除非您需要将append更改为add)。

您现在可以调整代码吗?

答案 1 :(得分:1)

使用firstlist,你可以混合列表的概念和字典的概念。看起来你只想要一个清单......

firstlist = ['Joe']

,而不是使用has_key,写

if printthis in firstlist:

答案 2 :(得分: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"

答案 3 :(得分:0)

@Ricky Mason:我稍微修改了一下代码。在这里,您需要一个dict对象,而不是一个集合或列表。一个dict对象#since有一个键值对很容易检索你在checkmenu中寻找的值,同时#time你也可以从列表中删除值。我希望这有帮助。随意发布有关相同的任何进一步查询。

以下是回复中的代码