-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitcoin.py
38 lines (28 loc) · 847 Bytes
/
bitcoin.py
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
33
34
35
36
37
38
import json
import requests
import sys
def main():
bitcoins = get_n_of_bitcoins()
rate = get_rate()
print(bitcoin_cost(bitcoins, rate))
def bitcoin_cost(n, rate):
cost = n * rate
return f"${cost:,.4f}"
def get_rate():
try:
r = requests.get("https://api.coindesk.com/v1/bpi/currentprice.json")
r_json = r.json()
return r_json["bpi"]["USD"]["rate_float"]
except requests.RequestException:
sys.exit("Error with file")
def get_n_of_bitcoins():
try:
# Get n of bitcoins in command-line argument and convert to float
bitcoins = float(sys.argv[1])
return bitcoins
except IndexError:
sys.exit("Missing command-line argument")
except ValueError:
sys.exit("Command-line argument is not a number")
if __name__ == "__main__":
main()