import requests
class RealTimeCurrencyConverter():
def __init__(self,url):
self.data= requests.get(url).json()
self.currencies = self.data['rates']
def convert(self, from_currency, to_currency, amount):
initial_amount = amount
# first convert it into USD if it is not in USD.
# because our base currency is USD
if from_currency != 'USD':
amount = amount / self.currencies[from_currency]
# limiting the precision to 4 decimal places
amount = round(amount * self.currencies[to_currency], 4)
return amount
def list():
print(' AOA - Angola Kwanza, ARS - Peso Argentino, EUR - Euro, '
'USD- Dólar Americano, '
'INR- Rupia Indiana, BRL - Real Brasileiro,'
' AUD- Dólar Australiano,\n GBP- Libras,'
'COP - Peso Colombiano, HUF- Florin, VES- Venuzuela,'
' JPY- Iene \n Listas de moedas '
'disponíveis consulte o seguinte site:'
' https://v6.exchangerate-api.com/v6/41bf75126e55373d5c05c31f/latest/USD' )
def converter():
try:
url = 'https://api.exchangerate-api.com/v4/latest/USD'
converter = RealTimeCurrencyConverter(url)
list()
moeda_inicial = input(" De: ").upper()
list()
moeda_final = input(" Para: ").upper()
valor = float(input("Valor a converter:"))
ar = round(converter.convert(moeda_inicial, moeda_final, valor), 2)
print(f'\n\033[1;33m {moeda_inicial} {valor} ------>'
f' {moeda_final} {ar}\033[m')
except:
print("\033[1;31m Opa! entrada invalida :(\33[0;0m")
converter()
escolha=True
while escolha:
txt='Converter moedas'
print(' -' * len(txt))
print('\t\t\033[34m' + txt + '\033[0;0m')
print(' -' * len(txt))
print("""
1.Converter moedas
2.Exit/Quit/Saída
""")
escolha= input("Escolha uma opção: ")
if escolha=="1":
print('\n')
converter()
elif escolha=="2":
print("\n Fim do Programa")
escolha = None
else:
print("\n \033[1;31m Escolha não válida.\nTente outra vez.\33[0;0m")
Comentários
Enviar um comentário