The Converter
Lesson 5 of 5 in Coddy's Currency Converter - Python Project course.
Let's create the core of this project - the Converter!
Challenge
EasyAdd a function named convert which gets from currency, amount and to currency, the function will print the equivalent value in "to currency" value.
Use the two helper functions you created, from_usd and to_usd.
If error encountered you should print it's message, you can do it like this (wrapping your code with try-except),
try:
# main code
except Exception as e:
print(e)Check the test cases for the needed output format.
Try it yourself
CURRENCIES = {
'USD': 1,
'EUR': 1.06,
'YEN': 0.0067,
'GBP': 1.23,
'AUD': 0.64,
'CAD': 0.74
}
def to_usd(from_currency, amount):
if from_currency not in CURRENCIES:
raise Exception(f'{from_currency} is not supported')
elif amount < 0:
raise Exception(f'Invalid amount')
else:
res = CURRENCIES[from_currency] * amount
return res
def from_usd(to_currency, amount):
if to_currency not in CURRENCIES:
raise Exception(f'{to_currency} is not supported')
elif amount < 0:
raise Exception(f'Invalid amount')
else:
res = amount / CURRENCIES[to_currency]
return round(res, 4)