ubuntu2004
import requests1import json23baseUrl = 'http://apilayer.net/api/'45# Read the key6with open("access_key.txt") as keyfile:7key = keyfile.readline().strip()89# Load the list of available currencies10listUrl = baseUrl + 'list'11response = requests.get(listUrl, params={'access_key': key, })12# Convert the response into a Python dictionary13# The 'Types' chapter in the book describes dictionaries14currency_dictionary = json.loads(response.content)['currencies']1516# Print them all out (for now, this could be better later!)17for code in sorted(currency_dictionary.keys()):18# The encoding stuff here is because the currency names have19# Unicode characters that can't be printed20print code, currency_dictionary[code].encode('ascii', 'ignore')2122# Ask the user for an amount in dollars to convert23amount = float(raw_input('How much money do you want to convert? '))2425# Ask the user for the code for the country they want26country_code = raw_input('Enter code for the country you want: ')2728# Request the current exchange rate for that country29quoteUrl = baseUrl + 'live'3031response = requests.get(quoteUrl, params={'access_key': key, 'currencies': country_code})3233rate = json.loads(response.content)['quotes']['USD' + country_code]3435print amount, 'American dollars is', amount*rate, currency_dictionary[country_code]3637383940