No more “Hello World!”
“Hello World!” program is golden for all beginning coders, I did write it a lot in the past as well. But, we are no longer ‘beginners’. We have passed though 10s coder and coder who knows how to ask for help, we deserve better. So we won’t write “Hello World!” within 10 lines of code, we write it within 10 seconds instead. For 10 lines of code, we can write a daily life solution. Thus, we write a simply but fully functional currency converter in Python.
Remember what we have learnt
On last post, we learnt how to import and use the “requests” library in Python. By using “requests” library, we can obtain http response from other URLs, that means, we can get information from APIs(Application Programming Interface, or you could say, “a gateway for information exchange”) on the internet. In order to build our currency converter, we need a source for providing foreign exchange rates. Then we use Fixer.io , a free JSON API providing rates published by the European Central Bank.
Put it all together within 10 lines
Python, data of foreign exchange rates and library to get information from the internet, we are ready to make our own currency converter. Use your favorite code editor, or a simply text editor, to type following lines of code:
import requests
from_curr = input("From currency: ").upper()
to_curr = input("To currency: ").upper()
amount = float(input("Amount: "))
response = requests.get("http://api.fixer.io/latest?base="+from_curr+"&symbols="+to_curr)
rate = response.json()['rates'][to_curr]
print("Exchange rate: "+ str(round(rate,4))+", "+str(amount)+" "+from_curr+" = " + str(round((rate * amount), 2)) + " " +to_curr)
Save the above code as a .py file (e.g 10liner_fx.py) then run it with the python command.
python 10liner_fx.py
The program would provide an input interface for user to enter:
- currency converted from
- currency converted to
- amount to convert
The program will then print out the rate and converted currency.
Sample screenshot:
Code explained, line by line!
import requests
This is the “ask for help” command in Python. In this case, we are asking requests library to help us on handling request/response from the internet.
from_curr = input("From currency: ").upper()
to_curr = input("To currency: ").upper()
amount = float(input("Amount: "))
We receive user’s inputs using “input(…..)” command, while “.upper()” is used to convert all input to upper case and “float(…..)” is used to convert input to number with decimal.
response = requests.get("http://api.fixer.io/latest?base="+from_curr+"&symbols="+to_curr)
We are passing our input to Fixer.io API and get the response using requests library.
rate = response.json()['rates'][to_curr]
Fixer.io API returns the exchange rate in a JSON object, so we get the rate under their schema.
print("Exchange rate: "+ str(round(rate,4))+", "+str(amount)+" "+from_curr+" = " + str(round((rate * amount), 2)) + " " +to_curr)
On our last line of code, we use str(….) function to transform numeric values to strings before printing them out. Now we got rate, amount, currency from and to, print them all in a line then we got what we wanted.
That’s it! We even have 3 lines left to put comments on.
The complete source code can be found at https://github.com/codeastar/10liner_fx .
What we have learnt on this hands-on:
- the usage of input method
- the handling of 3rd parties’ API
- string and numeric values conversion