0

相当简单的问题,我想......我刚刚安装了 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

4 回答 4

1

示例中有多个错误,让我们逐一分析。首先,如果你想要一个列表,那么你需要这样定义它,即:

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

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

第三个问题与has_key. 如果使用的对象是 aset或 a list,则没有has_key为它们定义这样的方法。如果有问题的对象是 a ,那将起作用dict,但事实并非如此。检查给定代码中包含的正确方法是something in A. 在使用时执行此操作set比在使用时更有效list,并且代码保持不变(除非您需要更改appendadd)。

你现在可以调整你的代码吗?

于 2012-12-07T16:27:32.840 回答
1

使用 firstlist,您将列表的概念和字典的概念混合在一起。看起来你只是想要一个列表......

firstlist = ['Joe']

另外,不要使用 has_key,而是写

if printthis in firstlist:
于 2012-12-07T16:30:00.080 回答
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"
于 2012-12-07T18:12:27.047 回答
0

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

这是以下回复中的代码

于 2012-12-07T17:57:42.030 回答