Skip to content

Commit f816db5

Browse files
authored
Update currency.py (DhanushNehru#355)
1 parent a5b3c4d commit f816db5

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

Currency Script/currency.py

+26-25
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,34 @@
55
import requests
66

77
class Currency_convertor:
8-
# empty dict to store the conversion rates
9-
rates = {}
10-
def __init__(self, url):
11-
data = requests.get(url).json()
12-
13-
# Extracting only the rates from the json data
14-
self.rates = data["rates"]
15-
16-
# function to do a simple cross multiplication between
17-
# the amount and the conversion rates
18-
def convert(self, from_currency, to_currency, amount):
19-
initial_amount = amount
20-
if from_currency != 'EUR' :
21-
amount = amount / self.rates[from_currency]
22-
23-
# limiting the precision to 2 decimal places
24-
amount = round(amount * self.rates[to_currency], 2)
25-
print('{} {} = {} {}'.format(initial_amount, from_currency, amount, to_currency))
8+
# empty dict to store the conversion rates
9+
rates = {}
10+
11+
def __init__(self, url):
12+
data = requests.get(url).json()
13+
# Extracting only the rates from the json data
14+
self.rates = data["rates"]
15+
16+
# function to do a simple cross multiplication between
17+
# the amount and the conversion rates
18+
def convert(self, from_currency, to_currency, amount):
19+
initial_amount = amount
20+
if from_currency != 'EUR':
21+
amount = amount / self.rates[from_currency]
22+
23+
# limiting the precision to 2 decimal places
24+
amount = round(amount * self.rates[to_currency], 2)
25+
print('{} {} = {} {}'.format(initial_amount, from_currency, amount, to_currency))
2626

2727
# Driver code
2828
if __name__ == "__main__":
2929

30-
# YOUR_ACCESS_KEY = 'GET YOUR ACCESS KEY FROM fixer.io'
31-
url = str.__add__('http://data.fixer.io/api/latest?access_key=', YOUR_ACCESS_KEY)
32-
c = Currency_convertor(url)
33-
from_country = input("From Country: ")
34-
to_country = input("TO Country: ")
35-
amount = int(input("Amount: "))
30+
YOUR_ACCESS_KEY = 'YOUR_ACCESS_KEY_HERE' # Define your access key
31+
url = f'http://data.fixer.io/api/latest?access_key={YOUR_ACCESS_KEY}' # Use f-string for cleaner concatenation
32+
c = Currency_convertor(url)
33+
34+
from_country = input("From Country (currency code): ")
35+
to_country = input("To Country (currency code): ")
36+
amount = float(input("Amount: ")) # Use float for decimal support
3637

37-
c.convert(from_country, to_country, amount)
38+
c.convert(from_country, to_country, amount)

0 commit comments

Comments
 (0)