Python Currency Converter

Really wanted a way to handle currency and exchange rates. The thing is that a lot of the solutions were based on Python2 libraries. The one I am going to share now is based on Python 3. So please don’t bother me with questions of why this isn’t working with Python 2.

import urllib.request
import json

def currency_converter(currency_from, currency_to, currency_input):
    yql_base_url = ""
    yql_query = 'select%20*' \
    yql_query_url = yql_base_url + "?q=" + yql_query + "&format=json&" \
        yql_response = urllib.request.urlopen(yql_query_url)
            json_string = str(
            json_string = json_string[2:]
            json_string = json_string[:-1]
            yql_json = json.loads(json_string)
            last_rate = yql_json['query']['results']['rate']['Rate']
            currency_output = currency_input * float(last_rate)
            return currency_output
        except (ValueError, KeyError, TypeError):
            return "JSON format error"

    except IOError as e:

currency_input = 1
# currency codes :
currency_from = "USD"
currency_to = "TRY"
rate = currency_converter(currency_from, currency_to, currency_input)

Hope this will be helpful.

Have fun!

John Roach

John Roach

Westlake, OH, USA