# How to sum the values of specific keys while iterating over a dictionary in python?

Create a solution that accepts an integer input identifying how many
shares of stock are to be purchased from the Old Town Stock Exchange,
followed by an equivalent number of string inputs representing the
stock selections. The following dictionary stock lists available stock
selections as the key with the cost per selection as the value.

``````stocks = {'TSLA': 912.86 , 'BBBY': 24.84, 'AAPL': 174.26, 'SOFI': 6.92, 'KIRK': 8.72, 'AURA': 22.12, 'AMZN': 141.28, 'EMBK': 12.29, 'LVLU': 2.33}

Output the total cost of the purchased shares of stock to two decimal places.

The solution output should be in the format

Total price: \$cost_of_stocks
Sample Input/Output:
If the input is

3
SOFI
AMZN
LVLU
then the expected output is

Total price: \$150.53"
``````

Hi everyone, working on some practice assignments in zybooks and this one is consistently stumping me.

``````stocks = {'TSLA': 912.86, 'BBBY': 24.84, 'AAPL': 174.26, 'SOFI': 6.92,
'KIRK': 8.72, 'AURA': 22.12, 'AMZN': 141.28, 'EMBK': 12.29, 'LVLU': 2.33}
selections = int(input())
i = 0
for i in range(selections):
choices = (input())
if choices in stocks:
print((stocks[choices]))
``````

This is currently what I have tested and it outputs the correct values of whatever Key I enter as well as whatever number of keys I want to enter

``````IE
input
3
TSLA
BBBY
AMZN

output
912.86
24.84
141.28
``````

But I cannot use `sum()` as it gives me a type error. How would I go about getting a specific number of inputs from the user, assigning that to a loop so it only iterates as many times as the user specified, and then outputting the SUM of the values associated with the keys the user inputs?

Thank you ðŸ™‚

### >Solution :

You need a variable like `res` to keep adding up your current result.

Also, it’s better to include the `input()` for selected stocks’ names outside the loop and stores them in an appropriate data structure, like an array, in this case.

``````stocks = {'TSLA': 912.86, 'BBBY': 24.84, 'AAPL': 174.26, 'SOFI': 6.92, 'KIRK': 8.72, 'AURA': 22.12, 'AMZN': 141.28,
'EMBK': 12.29, 'LVLU': 2.33}

number_of_selection = int(input())
stock_selection = [input() for i in range(number_of_selection)]

res = 0
for stock in stock_selection:
res += stocks[stock]

print(f'Total price: \${res}')
``````