-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcurrency.py
More file actions
31 lines (24 loc) · 818 Bytes
/
currency.py
File metadata and controls
31 lines (24 loc) · 818 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
import requests
API_KEY = 'fca_live_QiRD4yaE7K1aJQIAfVLojfu8J2KPxSXPYkDxxTzc'
BASE_URL = f"https://api.freecurrencyapi.com/v1/latest?apikey={API_KEY}"
CURRENCIES = ["USD", "CAD", "EUR", "AUD", "CNY"]
def convert_currency(base):
currencies = ",".join(CURRENCIES)
url = f"{BASE_URL}&base_currency={base}¤cies={currencies}"
try:
response = requests.get(url)
data = response.json()
return data["data"]
except:
print("Invalid currency.")
return None
while True:
base = input("Enter the base currency (q for quit): ").upper()
if base == "Q":
break
data = convert_currency(base)
if not data:
continue
del data[base]
for ticker, value in data.items():
print(f"{ticker}: {value}")