Write simple currency converter in Python!

Introduction

For some time I have been learning Python. After many times I have started learning to code, finally I have discovered python, easy to learn, easy to use, and cross platform programming language. I'm not going to make another "Hello World" tutorial don't worry.

For all interested you can read a lot more about python on this links:

Prerequisites

You will need to have python 2.7 installed, witch you probably have since you read this far, and requests library. According to authors of this library "Requests is an Apache2 Licensed HTTP library, written in Python, for human beings.", and you can git clone https://github.com/kennethreitz/requests.

Currency Converter

Why currency converter? I get bored with all "Hello World!" tutorials, and thought something useful would be nice. Some time I need to convert some currency when doing online orders, and since I have so many bookmarks, it gets hard to find currency converter fast, so here we will code one with python, it will get data from two sources from internet, that should keep converted value up to date.

Two site's I have chosen for converter to get data from are:

I'm not sure but I think the first one is updated daily while second updates more frequently. Now if you visit first site for exchange rate's EUR to USD http://rate-exchange.appspot.com/currency?from=EUR&to=USD&q=1, and second site http://themoneyconverter.com/EUR/USD.aspx , you will see that we will need two different ways to extract data from those sites.

Now let's start writing our converter:

import requests

a = raw_input('Enter currency to convert from?')
a = a.upper()

b = raw_input('Enter currency to convert to?')
b = b.upper()

c = float(raw_input('Enter value to convert?'))

I think most of this dose not need explanation, but here it is, first we import requests library, after that we make few variables that will prompt user for input, and to make this case not sensitive we return variable like in upper case witch is the format we need for URL's.

Now let's define URL's from witch we will be getting data:

url = ('https://currency-api.appspot.com/api/%s/%s.json') % (a, b)

urlalt = ('http://themoneyconverter.com/%s/%s.aspx') % (a, b)

I have used real URL and added our user input's with %s in that string. The %s token allows me to insert and add variable to a string. Notice that the %s token is replaced by whatever is passed to the string after the % symbol. If you have only one variable you don't need () you can just type % a. Notice that I haven't used some long variable name's like everyone like to use so everything looks more complex then it actually is. Now we will use requests library to read URL's, add code like this:

import requests

a = raw_input('Enter currency to convert from?')
a = a.upper()

b = raw_input('Enter currency to convert to?')
b = b.upper()

c = float(raw_input('Enter value to convert?'))

url = ('https://currency-api.appspot.com/api/%s/%s.json') % (a, b)
print url

urlalt = ('http://themoneyconverter.com/%s/%s.aspx') % (a, b)
print urlalt

I think this part is clear, only thing you don't actually need print url and print urlalt, but it's good to have them so you can manually check is your code ok. And final step is to take data from this URL's, first url uses json() and we will use python ability to get value from key, and for second url we will use split and strip built in function in python. Here is complete code:

import requests

a = raw_input('Enter currency to convert from?')
a = a.upper()

b = raw_input('Enter currency to convert to?')
b = b.upper()

c = float(raw_input('Enter value to convert?'))

url = ('https://currency-api.appspot.com/api/%s/%s.json') % (a, b)
print url

r = requests.get(url)
print r.json()['rate']

print c*float(r.json()['rate'])

urlalt = ('http://themoneyconverter.com/%s/%s.aspx') % (a, b)
print urlalt

#split and strip
split1 = ('>%s/%s =') % (b, a)
strip1 = ('</textarea>')

ralt = requests.get(urlalt)
d = float(ralt.text.split(split1)[1].split(strip1)[0].strip())
print d

print c * d

You can find code for this on github: https://github.com/boxcontrol/currency_converter . Its missing some more case's , when currency is not found for example. It could be coded with function's, but for start I wanted to keep it as simple as possible.

To see how to make GUI and webapp for this code visit:

Currency Converter GUI

Currency converter WebApp

Comments !

social