Currency Converter

currenciesOur 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)

JSON Output

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.

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.

Share Button
Tagged with: ,