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 :)
CodePudding user response:
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 i in range(number_of_selection)]
res = 0
for stock in stock_selection:
res = stocks[stock]
print(f'Total price: ${res}')
CodePudding user response:
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)))
CodePudding user response:
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}" )