gpt4 book ai didi

python - 使用字典,练习代码

转载 作者:行者123 更新时间:2023-12-01 05:46:44 25 4
gpt4 key购买 nike

对于我之前提出的问题,我深表歉意,因为这些问题含糊不清且难以回答。我对编程还很陌生,并且仍在学习它的来龙去脉。所以请耐心听我说。现在介绍背景信息。我正在使用 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)

最佳答案

这些是一些细微的变化。对于字典:您正确使用它们。

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

关于python - 使用字典,练习代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15852757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com