forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto_price.py
More file actions
32 lines (23 loc) · 756 Bytes
/
Copy pathcrypto_price.py
File metadata and controls
32 lines (23 loc) · 756 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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")