Reputation: 91
I have a list of stock prices as seen below. I want to prompt the user for a minimum and maximum stock price to invest in. Lets say the customer is willing to invest their money in stocks valued between $50 and $100. From the list below it should print to the screen all the stocks from the stock_price list below between that range with the stock price:symbol from the stock_symbol list. Then the user can choose a stock from that list to invest their money in.
# Stock lists for price and symbol
stock_price = [37.10, 80.07, 82.12, 93.27, 134.45, 131.05, 51.76, 114.63, 145.64, 77.99, 46.18]
stock_symbol = ['T', 'XOM', 'MDT', 'PG', 'JNJ', 'ECL', 'ABT', 'CVX', 'ITW', 'LOW', 'KO']
stockRangeMin = (float(input("Enter the value for a minimum stock to purchase")
stockRangeMax = (float(input("Enter the value for a maximum stock to purchase")
I'm not sure what to enter here to pring the stocks within the range the user specified above
stockChoice = (input("Enter the stock symbol you want to invest in")
investing = (float(input(f"Enter amount you'd like to invest in purchasing {stockChoice} today")
stockPurchased = investing/stockChoice
print ("Congrats, you have purchased {stockPurchased} shares of {stockChoice} with your investment amount of {investing}")
stockRangeMin = (float(input("Enter the value for a minimum stock to purchase? $")))
stockRangeMax = (float(input("Enter the value for a maximum stock to purchase? $")))
analysis = [symbol for price, symbol in sorted(zip(stock_price, stock_symbol)) if stockRangeMin <= price <= stockRangeMax]
print (f"\nThe Stock Symbols in your specified range of ${stockRangeMin} and ${stockRangeMax} are: \n",analysis)
stockChoice = (str(input("Enter the stock symbol you want to invest in")))
stockPurchased = investing/analysis.price
print ("Congrats, you have purchased {stockPurchased} shares of {stockChoice} with your investment amount of {investing}")
break
Upvotes: 0
Views: 192
Reputation: 60984
Here's a list comprehension that goes through the zipped lists and checks the prices
[symbol for price, symbol in zip(stock_price, stock_symbol) if stockRangeMin <= price <= stockRangeMax]
Upvotes: 1