2012-12-07 52 views
0

非常简单的问题,我想......我从字面上刚刚安装了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") 
+1

你从哪里得到从这个例子的代码?这在多方面是错误的。'firstlist'可以是一个集合或者一个列表,但是定义对于后者来说是错误的,并且与名称不匹配。鉴于'print'语句的格式化,您使用的是Python 2,因此您通常必须使用raw_input而不是输入并将结果转换为专用。 'input()'和'eval(raw_input())'是一样的,这就是你得到这样一个错误的原因。 – mmgp

+0

我根据其他示例制作了代码。只是帮助我弄清楚它是如何工作的。我不知道'set'是什么......还没有到达那里呢!我相信我正在使用Python 2.'eval(raw_input())'似乎没有摆脱错误。 –

+1

@RickyMason他并不是说你应该使用'eval(raw_input())',他说这和你现在正在做的事情是一样的。由于您使用的是Python 2.x,所以只需使用'raw_input()'。 –

回答

1

有在本例中的多个错误,让我们通过他们去。首先,如果你想有一个列表,那么你需要将它定义为这样的,即:

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

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

第三个问题与has_key有关。如果使用的对象是setlist,那么没有为它们定义的方法has_key。如果有问题的对象是dict,那就行了,但事实并非如此。在给定的代码中检查遏制的正确方法是something in A。在使用set时使用set比使用list时效率更高,且代码保持不变(除非需要将append更改为add)。

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

+0

谢谢。你刚才的评论终于打开了我,因为我正在浏览原始教程。这帮了很多。 –

+0

这很好,Python的教程和它的文档通常是一个很好的去处。 – mmgp

1

有了firstlist,你就可以混合列表的想法和字典的想法。它看起来像你只想要一个列表...

firstlist = ['Joe'] 

也,而不是使用对象的has_key,写

if printthis in firstlist: 
0

@瑞奇·梅森:我已经修改了代码位。在这里你需要一个字典对象而不是一个集合或一个列表。字典对象#since有一个键值对可以很容易地检索您在checkmenu中查找的值,并且在同一时间您可以从列表中删除值。我希望这有帮助。随意发布任何关于相同的进一步查询。

以下是答复如下

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"