question 171 Exceptions & Validation · easy
Read a money amount from a spreadsheet
A finance spreadsheet exports amounts as text such as "$1,234.50". Before totals can be added up, each one must become a number, and the ones that can't be read must be marked rather than guessed.
Function
parse_money(text)Cleaningremove spaces at the ends, then one leading
$ if present, then every commaReturnsthe amount as a float, or
None when the cleaned text isn't a numberExamples
callparse_money('$1,234.50')returns1234.5
callparse_money('12.93')returns12.93
callparse_money('N/A')returnsNone