[Tutor] dollarize.py

wesley chun wescpy at gmail.com
Sat Jun 21 09:14:03 CEST 2008


> dollarize(1234567.8901) returns-> $1,234,567,89
>    :
> amount = raw_input("Enter an amount: ")
> dollar_amount = dollarize(amount)
> print dollar_amount


the solution you're creating is *slightly* different than the original
spec in the problem (Exercise 13-3). the argument to dollarize() is
supposed to be a *float*, not a string. other than that, you're well
on your way!

also, be aware of the typo in the class skeleton provided on p. 619.
the published changed the "`"s to "'"s on line 13.  since the backtick
quotes are going away in Python 3, i would just suggest changing
"return `self.value`" to "return repr(self.value)".

aside from this, the way i usually solve this problem is to save off
the sign, round off the fraction/pennies, and then break it up into
dollars and cents, then process the commas into the dollar amount, and
finally merge everything back together with the sign and the $.

best of luck!
-- wesley

ps. marty's use of locale is a great add-on to this problem. i've had
some students go this distance and implement it in a similar way but
not very often.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
 http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list