我无法弄清楚如何将某些值添加到列表中的每个单独的密钥。我有几个类型(b,m,t,d,c),它们是键,然后我想将这些项目的成本添加到每次通过循环时都是字典值的列表中。 这是我到目前为止有:Python:添加一个项目列表,这是一个字典中的值列表
a={}
allitemcostb=[]
allitemcostm=[]
allitemcostt=[]
allitemcostd=[]
allitemcostc=[]
n=4
while n>0:
itemtype=raw_input("enter the item type-b,m,t,d,c:")
itemcost=input("enter the item cost:")
if itemtype="b":
allitemcostb.append(itemcost)
a[itemtype]=allitemcostb
if itemtype="m":
allitemcostm.append(itemcost)
a[itemtype]=allitemcostm
if itemtype="t":
allitemcostt.append(itemcost)
a[itemtype]=allitemcostt
if itemtype="d":
allitemcostd.append(itemcost)
a[itemtype]=allitemcostd
if itemtype="c":
allitemcostc.append(itemcost)
a[itemtype]=allitemcostc
else:
print "Sorry please enter a valid type"
n=n-1
print a
它不断给我的错误信息,无论是东西没有定义,或者语法不正确。 感谢
我不知道关于蟒蛇,但你有看到过吗? https://docs.python.org/2/tutorial/datastructures.html#dictionaries – artsylar