简体   繁体   中英

print sum of 2 dictionary values based on key

My code looks like:

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

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

I'm getting an error of :

addition1 = coins_in_the_bag[0]
KeyError: 0

Thanks!

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

coins_in_the_bag is a dict . Its contents are accessed using keys such as

>>> coins_in_the_bag['Bug Out Bag']
# ['q', 0.25, 100]
>>> coins_in_the_bag['XM']
# ['q', 0.25, 10]

Also, coins_in_the_bag['Bug Out Bag'] would return a list. Be alert to call float on a number, and not the whole list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM