Skip to content

Commit 5f4df3b

Browse files
committed
add basic crypto price converter in accordance with #13008
1 parent a71618f commit 5f4df3b

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

web_programming/crypto_price.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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")

0 commit comments

Comments
 (0)