Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions web_programming/crypto_price.py
Original file line number Diff line number Diff line change
@@ -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")
Loading