currency-converter

Hosted on github - https://github.com/blurer/currency-converter

  • https://pypi.org/project/forex-python/ required - pip3 install forex-python

  • Cleaned up the code quite a bit.

  • Default to convert from USD if no input given.

  • Default to convert to JPY if no input given.

  • Default to 1 if no input given.

Code:

#!/usr/bin/env python3
from forex_python.converter import CurrencyRates

c = CurrencyRates()
print('--------------------')
currency = input('Currency [USD]: ')
tocurrency = input('Convert to [JPY]: ')
startingValue = input('How much [1]: ')
if startingValue == '':
    startingValue = float(1)
else:
    startingValue = float(startingValue)

if currency == '':
    startingUnit = "USD"
else:
    startingUnit = str.upper(currency)

if tocurrency == '':
    convertUnit = "JPY"
else:
    convertUnit = str.upper(tocurrency)
print('--------------------')

fxrate = c.get_rate(startingUnit , convertUnit)
convertedMath = round(startingValue * fxrate, 2)

print(convertedMath)

Usage

--------------------
Currency [USD]:  
Convert to [JPY]: 
How much [1]: 
--------------------
107.1
--------------------
Currency [USD]:    
Convert to [JPY]: PHP
How much [1]: 10000
--------------------
494997.37