Menu
Coddy logo textTech

Handle errors

Lesson 3 of 5 in Coddy's Currency Converter - Python Project course.

It's a good practice to handle errors.

In Python to raise an exception with an error message we use,

raise Exception("My error message")
challenge icon

Challenge

Easy

Add error handling to to_usd function, handle the above errors:

  • Currency not supported - raised if the currency is not in CURRENCIES
  • Invalid amount - raised if the amount asked is lower than 0

Check the test cases for the error message format of each error!

Try it yourself

CURRENCIES = {
    'USD': 1,
    'EUR': 1.06,
    'YEN': 0.0067,
    'GBP': 1.23,
    'AUD': 0.64,
    'CAD': 0.74
}


def to_usd(currency, amount):
    res = CURRENCIES[currency] * amount
    return res

All lessons in Currency Converter - Python Project