File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ """
2+ Convert ETH to USD using real-time price data from CoinGecko.
3+ """
4+
5+ # /// script
6+ # requires-python = ">=3.10"
7+ # dependencies = [
8+ # "httpx",
9+ # ]
10+ # ///
11+
12+ import httpx
13+
14+ COINGECKO_URL = "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd"
15+
16+ def get_eth_price_usd () -> float :
17+ """Fetch the current ETH price in USD."""
18+ response = httpx .get (COINGECKO_URL , timeout = 10 )
19+ response .raise_for_status ()
20+ data = response .json ()
21+ return data ["ethereum" ]["usd" ]
22+
23+ def eth_to_usd (eth_amount : float ) -> float :
24+ """Convert ETH amount to USD."""
25+ return eth_amount * get_eth_price_usd ()
26+
27+ if __name__ == "__main__" :
28+ eth_amount = float (input ("Enter ETH amount: " ))
29+ usd_value = eth_to_usd (eth_amount )
30+ print (f"{ eth_amount } ETH = ${ usd_value :.2f} USD" )
You can’t perform that action at this time.
0 commit comments