Exercise: Stringers

Money is usually stored as whole cents in an int, because floats can't hold 0.10 exactly. People still want to read dollars.

Exercise: give Money two methods.

  • __repr__ returns code that rebuilds it: Money(1234).
  • __str__ returns dollars for people: $12.34. Always show two digits of cents ($0.05), put commas in the thousands ($1,234,567.89), and put the minus sign first (-$5.05).

Hints: divmod(abs(cents), 100) splits dollars from cents. In an f-string, {n:,} adds commas and {n:02d} pads to two digits.

Press Check to run the tests.