diff --git a/web_programming/crypto_price.py b/web_programming/crypto_price.py new file mode 100644 index 000000000000..a3188932256c --- /dev/null +++ b/web_programming/crypto_price.py @@ -0,0 +1,32 @@ +""" +Convert ETH to USD using real-time price data from CoinGecko. +""" + +# /// script +# requires-python = ">=3.10" +# dependencies = [ +# "httpx2", +# ] +# /// + +from httpx2 import get + +COINGECKO_URL = ( + "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd" +) + + +def get_eth_price_usd() -> float: + """Fetch the current ETH price in USD.""" + return get(COINGECKO_URL, timeout=10).raise_for_status().json()["ethereum"]["usd"] + + +def eth_to_usd(eth_amount: float) -> float: + """Convert ETH amount to USD.""" + return eth_amount * get_eth_price_usd() + + +if __name__ == "__main__": + eth_amount = float(input("Enter ETH amount: ")) + usd_value = eth_to_usd(eth_amount) + print(f"{eth_amount} ETH = ${usd_value:.2f} USD")