Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update currency.py #317

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 47 additions & 31 deletions Currency Script/currency.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,53 @@
# Python program to convert the currency
# Python program to convert the currency
# of one country to that of another country

# Import the modules needed
# Import the required module
import requests

# Currency converter class
class Currency_convertor:
# empty dict to store the conversion rates
rates = {}
def __init__(self, url):
data = requests.get(url).json()

# Extracting only the rates from the json data
self.rates = data["rates"]

# function to do a simple cross multiplication between
# the amount and the conversion rates
def convert(self, from_currency, to_currency, amount):
initial_amount = amount
if from_currency != 'EUR' :
amount = amount / self.rates[from_currency]

# limiting the precision to 2 decimal places
amount = round(amount * self.rates[to_currency], 2)
print('{} {} = {} {}'.format(initial_amount, from_currency, amount, to_currency))

# Driver code
# Empty dictionary to store the conversion rates
rates = {}

# Constructor to initialize the conversion rates from an API
def __init__(self, url):
# Fetch the data from the provided URL and store it as a JSON object
data = requests.get(url).json()

# Extract only the 'rates' section from the JSON data
self.rates = data["rates"]

# Function to convert from one currency to another
# using simple cross-multiplication based on the rates
def convert(self, from_currency, to_currency, amount):
initial_amount = amount # Store the initial amount before conversion

# If the base currency is not EUR, first convert the amount to EUR
if from_currency != 'EUR':
amount = amount / self.rates[from_currency]

# Convert the amount to the target currency and round it to 2 decimal places
amount = round(amount * self.rates[to_currency], 2)

# Display the result in a readable format
print('{} {} = {} {}'.format(initial_amount, from_currency, amount, to_currency))

# Driver code to test the functionality
if __name__ == "__main__":

# YOUR_ACCESS_KEY = 'GET YOUR ACCESS KEY FROM fixer.io'
url = str.__add__('http://data.fixer.io/api/latest?access_key=', YOUR_ACCESS_KEY)
c = Currency_convertor(url)
from_country = input("From Country: ")
to_country = input("TO Country: ")
amount = int(input("Amount: "))

c.convert(from_country, to_country, amount)

# Replace 'YOUR_ACCESS_KEY' with your actual access key from fixer.io
YOUR_ACCESS_KEY = 'GET_YOUR_ACCESS_KEY_FROM_fixer.io'

# Construct the API URL with the access key
url = str.__add__('http://data.fixer.io/api/latest?access_key=', YOUR_ACCESS_KEY)

# Create an instance of the Currency_convertor class
c = Currency_convertor(url)

# Take inputs from the user: source country, target country, and amount to convert
from_country = input("From Country (currency code): ")
to_country = input("To Country (currency code): ")
amount = int(input("Amount: "))

# Call the convert function to get the conversion result
c.convert(from_country, to_country, amount)