# Web App to Get Stock Market Data from AlphaVantage Service import requests #Function that gets stock info from API # Returns results in list of two strings # DO NOT EDIT THIS FUNCTION EXCEPT TO INSERT YOUR API KEY WHERE INDICATED def getStock(symbol): baseURL = "https://www.alphavantage.co/query? function=GLOBAL_QUOTE&datatype=csv" keyPart = "&apikey=" + "REPLACE WITH YOUR API KEY" #Add API key symbolPart = "&symbol=" + symbol stockResponse = requests.get(baseURL+keyPart+symbolPart) return stockResponse.text #Return only text part of response #Function that computes and displays results def main(): # Add code that meets the program specifications (see instructions in Canvas) #Code that starts the app main()
Transcribed Image Text:main() function
The starter code includes a main() function that is called when the app starts. Add code to
make it:
1. Start by displaying this message: "Check stock prices."
2. Ask the user for a stock symbol.
3. Call getStock() with the stock symbol, and save the returned stock-price data.
4. Split the data (two text lines) into a list of two strings (hint: use string method .split(/).
5. If the split data is a list of two strings (each in CSV format), the data is good and the
stock was found. (Hint: if the stock symbol was valid. the length of the split data should
be 2.) Extract the stock's previous closing price, opening price, high price, low price,
Prof Jeff johnson
CS 110
and current price. Do it in a way that will still work if AlphaVantage changes the order
of items in the returned data. E.g. don't assume that the stock's opening price is in the
second position (index 1) in the list, because if your program assumes that and
tomorrow AlphaVantage moves the opening price to another position, your app won't
work anymore. Write your app so it finds each price no matter how they are ordered,
and uses the appropriate index to get each price.
6. Print the previous closing price, opening price, high price, current price, and low price.
7. If the split data is not a list of exactly 2 items (CSV strings), that means the stock symbol
was not found. In that case, tell the user the stock symbol was not found, and then
continue with step 10 (below).
8. When the app displays prices for a stock (but not when it can't find a stock), it should
also write the same information to an output file stock_prices.txt. Each time the app
successfully displays the prices for a stock, the app should append the information to
the file. Eg., if the user requests prices for three stocks, when the application quits,
stock_prices.txt should contain info for those three stocks, with a line like this separating
them: ====
====
9. The app should not clear and replace the data in stock_prices.txt each time it is run. It
should keep appending stock prices to the file.
10. The program should ask the user "Check another stock price? (Y or N)", and should
perform input validation to make users enter a valid response. If the response is "Y" or
"y", the program should loop back to Step 2 (not Step 1) and ask for another stock
symbol. If the response is “N" or "n", the program closes the output file and quits.
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.