diff --git a/Currency_Converter/README.md b/Currency_Converter/README.md new file mode 100644 index 000000000..6821b14c1 --- /dev/null +++ b/Currency_Converter/README.md @@ -0,0 +1,14 @@ +# Currency Converter +A python script used to convert the currency of one country to that of another country. + +## Modules Used +- requests + +## How to Use +1. Install necessary requirements using +```bash +pip install -r requirements.txt +``` +2. We are using fixer.io API for live conversion rates. Get your access key from [here](https://fixer.io/) +3. Replace **API_KEY** with your own API Key. +4. Input the countries you want to convert respectively and amount to be converted. diff --git a/Currency_Converter/currency_converter.py b/Currency_Converter/currency_converter.py new file mode 100644 index 000000000..5aca03068 --- /dev/null +++ b/Currency_Converter/currency_converter.py @@ -0,0 +1,35 @@ +# Currency Converter + +# Import necessary modules +import requests + + +class Currency_Converter: + rate = {} + + def __init__(self, url): + data = requests.get(url).json() + # Extracting rate from json + self.rate = data["rates"] + + # Conversion + def convert(self, from_country, to_country, amount): + initial = amount + if from_country != 'EUR': + amount = amount / self.rates[from_country] + + # Precision to 2 decimal places + amount = round(amount * self.rate[to_country], 2) + print(f'{initial} {from_country} = {amount} {to_country}') + + +# Driver Code +if __name__ == "__main__": + # Replace API_KEY with your own API key from fixer.io + url = 'http://data.fixer.io/api/latest?access_key=API_KEY' + cc = Currency_Converter(url) + from_country = input("From: ") + to_country = input("To: ") + amount = int(input("Amount: ")) + + cc.convert(from_country, to_country, amount) diff --git a/Currency_Converter/requirements.txt b/Currency_Converter/requirements.txt new file mode 100644 index 000000000..f2293605c --- /dev/null +++ b/Currency_Converter/requirements.txt @@ -0,0 +1 @@ +requests