2013-04-06 23 views
1

我對以前的問題表示歉意,因爲它們含糊不清,難以回答。我對編程還是比較陌生的,現在仍然在學習它的全部內容。所以請耐心等待。現在到背景資料。我正在使用python 3.3.0。我已經將它加載到Eclipse IDE上,這就是我正在使用的代碼編寫和測試。使用字典,練習代碼

現在,我正在嘗試學習如何創建和使用字典。因此,我的任務是創建一個價格匹配代碼,通過用戶界面不僅可以搜索字典中的項目(這是鍵和與位置和價格相關的值)。到目前爲止我創建了一個運行良好的用戶界面,但沒有任何錯誤(至少在IDE中)當我運行並輸入所有提示時,空字典不會更新,因此我不能再撥打電話詞典爲早期輸入。

我有我在下面寫的代碼,並希望如果有人能告訴我,如果我正確地做事。如果有更好的方法去解決這個問題。我仍然在學習,所以圍繞代碼術語的更詳細的解釋會很有用。

print("let's Price match") 
decition = input("Are you adding to the price match list?") 
if decition == "yes": 
    pricematchlist = {"Snapple":["Tops",99]} 
    location = input("Now tell me where you shopped") 
    item = input("Now what was the item") 
    price = input("Now how much was the item") 
    int(price) 
    pricematchlist[item]=location,price 
    print(pricematchlist) 
else: 
    pricematchlist = {"Snapple":["Tops",99]} 
    reply = input("Ok so you want to search up a previous price?") 
    if reply == "yes": 
     search = input("What was the item?") 
     pricematchlist.item(search) 
+0

寫下這個問題將更適合代碼審查。如果您有特定的問題區域,請指出,我們可能會提供幫助。 – ecline6 2013-04-06 16:00:17

+1

請問一個問題,而不是兩個完全不同的東西。如果你需要幫助讓你的字典工作,然後嘗試編寫一個簡短的實例來重現問題。如果你想**工作**(!)代碼的反饋,[codereview.SE]確實是正確的地方,但這似乎並不是這種情況。 – Adam 2013-04-06 16:12:48

回答

2

這些是一些小的變化。對於字典:您正確使用它們。

print("let's Price match") 
pricemathlist = {"Snapple":["Tops", 99]} # assign it here 
decition = input("Are you adding to the price match list?").lower() #"Yes"-->"yes" 
if decition == "yes": 
    # pricematchlist = {"Snapple":["Tops",99]} 
    # If this whole code block is called repeatedly, you don't want to reassign it 
    location = input("Now tell me where you shopped") 
    item = input("Now what was the item") 
    price = int(input("Now how much was the item")) 
    # int(price) does nothing with reassigning price 
    pricematchlist[item]=location,price 
    print(pricematchlist) 
else:  
    reply = input("Ok so you want to search up a previous price?").lower() 
    if reply == "yes": 
     search = input("What was the item?") 
     print pricematchlist[search] # easier way of accessing a value 
+0

謝謝你的幫助。但問題很快。我如何讓程序實際上通過用戶界面添加新的值。假設我去沃爾瑪買了98美分的山露。所以我訪問該程序並在該位置添加物品和價格。然後它將如何將新密鑰以及值添加到字典中。也可以通過任何方式爲錢添加美元符號。謝謝,麻煩您了。 – Codenovice 2013-04-07 21:16:32

+0

要使用美元符號,您必須將該數字轉換爲字符串。要創建一個新的鍵值對:'pricematchlist ['Mountain Dew'] ='$ 0.98''。 – 2013-04-07 21:57:48