2012-12-07 54 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"