The forex_python module can grab currency exchange rates for you. The worlds currencies (euro, dollar, pounds, inr) have different values.
In todays world there are many many fiat currencies.
To list all currency rates you use:
#!/usr/bin/python3
from forex_python.converter import CurrencyRates
c = CurrencyRates()
c.get_rates('USD')
In Python you can make a converter. The program below converts dollars to euros:
#!/usr/bin/python3
from forex_python.converter import CurrencyRates
c = CurrencyRates()
rate = c.get_rate('USD', 'EUR')
print(rate)
price = input("Enter amount ($): ")
value = float(("{0:.2f}").format(float(price)*rate))
print("\u20ac " + str(value))
Lambda function
The later part you can do with a lambda function. This is easier to read in my opinion:
#!/usr/bin/python3
from forex_python.converter import CurrencyRates
c = CurrencyRates()
rate = c.get_rate('USD', 'EUR')
print(rate)
price = input("Enter amount ($): ")
v = lambda x : round(float(x)*rate,2)
y = v(price)
print("\u20ac " + str(y))
The Millionaire test
So are you a millionaire in any currency? Lets find out. The method get_rates() returns a dictionary.
Then we iterate over that dictionary with items(), which goes over every key value pair. Then apply the lambda function and if there's more than 1.000.000 in the currency you are a millionaire.
#!/usr/bin/python3
from forex_python.converter import CurrencyRates
c = CurrencyRates()
price = input("Enter amount ($): ")
coins = c.get_rates('USD')
for key, value in coins.items():
v = lambda x : round(float(x)*value,2)
y = v(price)
if y > 1000000:
print(str(key) + " " + str(y))
Example run:
Enter amount ($): 1000
IDR 14225107.43
KRW 1214958.4
Related links:
Top comments (1)
Thanks! I was searching for this yesterday.