Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # importing required libraries
- import requests
- import pycountry
- from _datetime import datetime
- from babel import numbers
- def error_sev():
- print("Sorry! There seems to be an error. Please check if your network is working")
- def error_inp():
- print("Sorry! There seems to be an error. Please check if the currencies entered are valid.")
- def currency_print(input_cur, output_cur, input_currency_name, output_currency_name, amount, rate):
- # printing out exchange rate
- print("The rate for {} to {} as on {} is: "
- .format(input_currency_name, output_currency_name, date.strftime("%d-%m-%Y")), end='')
- print(numbers.format_currency(1, input_cur, locale='en') + " = " +
- numbers.format_currency(rate, output_cur, locale='en'))
- # printing converted value
- print("\t", end='')
- print(numbers.format_currency(amount, input_cur, locale='en') + " = " +
- numbers.format_currency(amount * rate, output_cur, locale='en'))
- print('-'*100)
- # list of available currencies
- currencies = [
- 'USD', 'JPY', 'BGN', 'CZK', 'DKK', 'GBP', 'HUF', 'PLN', 'RON', 'SEK', 'CHF', 'NOK', 'HRK', 'RUB', 'TRY',
- 'AUD', 'BRL', 'CAD', 'CNY', 'HKD', 'IDR', 'ILS', 'INR', 'KRW', 'MXN', 'MYR', 'NZD', 'PHP', 'SGD', 'THB',
- 'ZAR', 'ISK'
- ]
- # printing the list of available currencies for the user
- print("Available currencies: ", end='')
- for item in sorted(currencies)[:-1]:
- print(item, end=', ')
- print(sorted(currencies)[-1])
- # taking user input
- try:
- amount = float(input("Enter amount: "))
- # checking for input errors
- except ValueError:
- print("Invalid input. Please enter only numbers.")
- else:
- # taking currency values as input from user
- input_cur = input("Enter base currency code: ").upper()
- output_cur = input("Enter desired currency code (leave blank for all currencies): ").upper()
- # if user has specified desired currency
- if output_cur != '':
- response_url = "http://api.fixer.io/latest?base={}&symbols={}".format(input_cur, output_cur)
- response = requests.get(response_url)
- # checking for validity of inputs
- if output_cur in currencies and input_cur in currencies:
- # checking for validity of server
- if response.status_code is 200:
- # parsing JSON response
- data = response.json()
- date = datetime.strptime(data['date'], "%Y-%m-%d")
- rate = data['rates'][output_cur]
- print('-' * 100)
- # Getting currency names
- input_currency_name = pycountry.currencies.get(alpha_3=input_cur).name
- output_currency_name = pycountry.currencies.get(alpha_3=output_cur).name
- currency_print(input_cur, output_cur, input_currency_name, output_currency_name, amount, rate)
- else:
- # printing a server error
- error_sev()
- else:
- # printing an input error
- error_inp()
- # if user has not specified desired currency, print out all conversions
- else:
- response_url = "http://api.fixer.io/latest?base={}".format(input_cur)
- response = requests.get(response_url)
- # checking for input validity
- if input_cur in currencies:
- # checking for validity of inputs and server
- if response.status_code is 200:
- # parsing JSON response
- data = response.json()
- date = datetime.strptime(data['date'], "%Y-%m-%d")
- print('-' * 100)
- # looping through all rates
- rates = data['rates']
- for rate in sorted(rates):
- cur_rate = rates[rate]
- input_currency_name = pycountry.currencies.get(alpha_3=input_cur).name
- output_currency_name = pycountry.currencies.get(alpha_3=rate).name
- print("{} ({})".format(output_currency_name, rate))
- print("\t", end='')
- currency_print(input_cur, rate, input_currency_name, output_currency_name, amount, cur_rate)
- else:
- # printing a server error
- error_sev()
- else:
- # printing an input error
- error_inp()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement