Put into functions
This commit is contained in:
parent
ee6884addd
commit
dfb505d41c
1 changed files with 27 additions and 17 deletions
|
@ -4,22 +4,32 @@ import argparse
|
|||
import requests
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("coin")
|
||||
parser.add_argument("-b", "--base", default="USD")
|
||||
parser.add_argument("-r", "--round", type=int, default=2)
|
||||
args = parser.parse_args()
|
||||
coin = args.coin.upper()
|
||||
base = args.base.upper()
|
||||
round_nb = args.round
|
||||
def parse_arguments():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("coin")
|
||||
parser.add_argument("-b", "--base", default="USD")
|
||||
parser.add_argument("-r", "--round", type=int, default=2)
|
||||
args = parser.parse_args()
|
||||
|
||||
req = requests.get(
|
||||
"https://api.coinranking.com/v1/public/coins?base={}&symbols={}".format(base, coin)
|
||||
)
|
||||
req_j = req.json()
|
||||
data = req_j["data"]
|
||||
base_sign = data["base"]["sign"]
|
||||
coin_data = data["coins"][0]
|
||||
price = float(coin_data["price"])
|
||||
return args
|
||||
|
||||
print("{} = {}{}".format(coin, base_sign, round(price, round_nb)))
|
||||
def compute(args):
|
||||
coin = args.coin.upper()
|
||||
base = args.base.upper()
|
||||
round_nb = args.round
|
||||
|
||||
req = requests.get(
|
||||
"https://api.coinranking.com/v1/public/coins?base={}&symbols={}".format(
|
||||
base, coin
|
||||
)
|
||||
)
|
||||
req_j = req.json()
|
||||
data = req_j["data"]
|
||||
base_sign = data["base"]["sign"]
|
||||
coin_data = data["coins"][0]
|
||||
price = float(coin_data["price"])
|
||||
|
||||
return "{} = {}{}".format(coin, base_sign, round(price, round_nb))
|
||||
|
||||
|
||||
print(compute(parse_arguments()))
|
||||
|
|
Loading…
Add table
Reference in a new issue