cyberjax21
cyberjax21

Reputation: 91

Find a range of numbers between values

Problem to Solve

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']

User stock filter price range

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

Ask the user for stock symbol to purchase from and the investment amount from the filtered list

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}")

The modified per your reply is working for filtering out the symbols in the price range specified

        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)

This part though I need to calculate correctly for stockPurchased. Not sure how to enter it. analysis.price and analysis[price] and analysis[0] doesn't work.

        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

Answers (1)

Patrick Haugh
Patrick Haugh

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

Related Questions