Write a python function that takes 'ticker' and returns the dollar value of the stock…
- User
Write a python function that takes 'ticker' and returns the dollar value of the stock specified by 'ticker'.
- Assistant
In order to retrieve the dollar value of a stock specified by its ticker symbol, you can use the
yfinancelibrary in Python. This library allows you to easily access financial data from Yahoo Finance. First, you need to install theyfinancelibrary using pip:pip install yfinanceNow, you can create a function that takes the ticker symbol as an input and returns the dollar value of the stock:
import yfinance as yf def get_stock_price(ticker): stock = yf.Ticker(ticker) todays_data = stock.history(period="1d") return todays_data['Close'][0] ticker = "AAPL" price = get_stock_price(ticker) print(f"The price of {ticker} stock is ${price:.2f}")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.