我为之前的问题道歉,因为它们含糊不清,难以回答。我仍然是编程方面的新手,我仍然在学习这一切的来龙去脉。所以请耐心等待。现在到背景资料。我正在使用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 :(得分: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