|
5 | 5 | import requests
|
6 | 6 |
|
7 | 7 | 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)) |
26 | 26 |
|
27 | 27 | # Driver code
|
28 | 28 | if __name__ == "__main__":
|
29 | 29 |
|
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 |
36 | 37 |
|
37 |
| - c.convert(from_country, to_country, amount) |
| 38 | + c.convert(from_country, to_country, amount) |
0 commit comments