基于键打印2个字典值的总和

时间:2015-12-09 19:31:03

标签: python dictionary

我的代码如下:

{'Bug Out Bag': ['q', 0.25, 100], 'XM': ['q', 0.25, 10]}

print('{}'.format(float(addition1)+float(addition2)) )

我收到错误:

addition1 = coins_in_the_bag[0]
KeyError: 0

谢谢!

1 个答案:

答案 0 :(得分:1)

coins_in_the_bag = {'Bug Out Bag': ['q', 0.25, 100], 'XM': ['q', 0.25, 10]}

coins_in_the_bagdict。使用诸如

之类的密钥访问其内容
>>> coins_in_the_bag['Bug Out Bag']
# ['q', 0.25, 100]
>>> coins_in_the_bag['XM']
# ['q', 0.25, 10]

此外,coins_in_the_bag['Bug Out Bag']会返回一个列表。警惕在号码上拨打float,而不是整个列表