runtime
259 / 500
question 259 Classes & Objects · medium

Money parsed by class methods

Prices arrive as text such as £1,250.10 or €19.99. Write a class Money that stores an amount as whole cents with a currency code, and two class methods that build Money from other formats. The currency symbols are a class attribute shared by both.

Class attributeSYMBOLS = {"£": "GBP", "$": "USD", "€": "EUR"}
`Money(cents, currency)`attributes cents (an int) and currency; ValueError if currency isn't one of the codes in SYMBOLS
`Money.parse(text)`strips spaces; the first character is the symbol; the rest is the amount, with , separators allowed; ValueError for an unknown symbol or an amount that isn't a number
`Money.from_amount(amount, currency="GBP")`an amount like 12.5 → 1250 cents (rounded to the nearest cent)

Loading your progress…

Money parsed by class methods — Python practice · Runtime