I need to multiply the values from each key and then add all the values together to print a single number. I know this probably super simple but i'm stuck
In my mind, I'd address this with something like:
for v in prices:
total = sum(v * (v in stock))
print total
But something like that isn't going to work :)
prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3 }
stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15 }
You could use a dict comprehension if you wanted the individuals:
>>> {k: prices[k]*stock[k] for k in prices}
{'orange': 48.0, 'pear': 45, 'banana': 24, 'apple': 0}
Or go straight to the total:
>>> sum(prices[k]*stock[k] for k in prices)
117.0