-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitcoin.py
More file actions
24 lines (20 loc) · 727 Bytes
/
bitcoin.py
File metadata and controls
24 lines (20 loc) · 727 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
import requests
def get_bitcoin_price():
"""
Fetches the current Bitcoin price from CoinDesk API.
"""
url = "https://api.coindesk.com/v1/bpi/currentprice.json"
try:
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data["bpi"]["USD"]["rate_float"]
else:
raise ConnectionError(f"API returned status code {response.status_code}")
except requests.RequestException:
raise ConnectionError("Failed to fetch Bitcoin price")
def calculate_value(amount, price):
"""
Calculates the total value of Bitcoin based on the amount and current price.
"""
return float(amount) * float(price)