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)
4

1 回答 1

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
于 2013-04-06T16:35:33.727 回答