-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency_converter.py
71 lines (55 loc) · 2.25 KB
/
currency_converter.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
from requests import get
from pprint import PrettyPrinter
BASE_URL = "https://api.freecurrencyapi.com"
API_KEY = "fca_live_RLRUV1zt7YhqW4FGSG1YDwAYIN1DaBjQZn49TkiD"
endpoint = f"/v1/latest?apikey={API_KEY}"
print("Welcome to CURRENCY CONVERTER")
print("-----------------------------")
print("List - lists the different currencies \nConvert - Convert from one currency to another \nRate - get the exchange rate of two currencies")
def get_currency():
printer = PrettyPrinter()
url = BASE_URL + endpoint
data = get(url).json()
printer.pprint(data)
def exchange_rate(cur1,cur2):
url = BASE_URL + endpoint + "¤cies=" + cur2 + "&base_currency=" + cur1
data = get(url).json()
rate = data['data']
print(rate)
value = rate[cur2]
print("-------------------------------------")
print(f"{cur1} -> {cur2} = {value} ")
print(F"One {cur1} is equal to {value} {cur2}")
print("-------------------------------------")
return value
def convert_currency(cur1,cur2,value):
#endpoint = f"/v1/latest?apikey={API_KEY}¤cies={cur2}&base_currency={cur1}"
conversion = exchange_rate(cur1,cur2)
product = conversion * float(value)
print(f"{value} {cur1} is equal to {product} {cur2}")
print("----------------------------------------------")
return product
def main():
while True:
choice = input("Enter a command (q to quit): ").lower()
if choice == "list" or "convert" or "rate":
if choice == "list":
get_currency()
if choice == "rate":
base = input("Enter Base currency: ").upper()
convert = input("Enter a currency to convert to: ").upper()
exchange_rate(base,convert)
if choice == "convert":
cur1 = input("Enter a base currency: " ).upper()
while True:
value = input(f"Enter an amount in {cur1}: ")
if value.isdigit():
cur2 = input("Enter a currency to convert to: ").upper()
convert_currency(cur1,cur2,value)
break
elif choice == "q":
print("Thank you see you :)")
break
else:
print("Accepted values List, convert, rate: ")