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

Question:

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 🙂

Asked By: Mpeon82

||

Answers:

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}

# integer input identifying how many shares of stock are to be purchased
cost_of_stocks, to_purchase = 0, int( input() )

while to_purchase > 0:
  # stock name input
  stock_selection = str( input() )

  # default value if not exists
  cost_of_stocks += stocks.get( stock_selection, 0 )

  to_purchase -= 1

print( f"Total price: ${cost_of_stocks:.2f}" )
Answered By: Alex Lanes

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

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.

Also, I assume that the input() is guaranteed valid, which is true for the website you mentioned.

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 _ in range(number_of_selection)]

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

print(f'Total price: ${res}')
Answered By: コリン

Your code does not add up the values of the stocks. You can try it like so:

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}

num_shares = int(input("Enter the number of shares you want to purchase: "))
total_price = 0

for i in range(num_shares):
  stock_name = input("Enter the name of the stock you want to purchase: ")

  if stock_name in stocks:
    total_price += stocks[stock_name]
  else:
    print("Invalid stock name")

print("Total price: $" + str(round(total_price, 2)))
Answered By: wp78de
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.