Our aim is to create a currency converter to help us convert a sum of money from one currency to another.
Currency exchange rates are constantly changing which is why we have decided against the idea of storing all the exchanges rates in our code are these would not remain up-to-date. Instead we will retrieve up-to-date currency exchange rates by making calls to an API that provides the current rates.
To do so we will use the “Currency Converter API” to retrieve up-to-date exchange rates. You can read more about this API on: https://currencyconverterapi.com/. For the purpose of this blog post we will use the free version of this API.
This API works as follows:
API Request: e.g. Converting from US Dollars (USD) to British Pounds (GBP)
https://free.currencyconverterapi.com/api/v7/convert?q=USD_GBP&compact=y
JSON Output
{"USD_GBP":{"val":0.786905}}
This API uses JSON to format the data. JSON (JavaScript Object Notation) is a popular lightweight data-interchange format. Its main benefit is that it is easy for humans to read and write and it is easy for machines to parse and generate as you can see in the code provided below. You can read more about JSON on https://www.json.org/
Python Code
Check our code to see how we make a call to the API and how we retrieve and extract the requested JSON data.
request a free API key from currencyconvertapi.com and insert this key on line 5 of the code below. Make sure you also follow instructions on how to verify your email to complete the API key request.
Your Task
Update the code above to accept a full range of currencies. You will need to use the API to retrieve a full list of currencies by making a request using the following URL: https://free.currencyconverterapi.com/api/v6/currencies
You will have to parse this JSON data to retrieve all the currency codes and append these to the list called validCurrencies currently declared on line 5 of the above trinket.
You may also want to output all the currency codes and the name of each currency to inform the user of the available codes and their meaning.
Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area