[Tutor] oops - resending as plain text

Dave Angel davea at davea.name
Wed Apr 17 00:09:47 CEST 2013


On 04/16/2013 05:46 PM, Jim Mooney wrote:
> Further question. If I round the input right at the beginning,
> round(paid,2) does that mean  I still have the original error from
> using .76 even before math, or does the rounding kill it? I would
> guess not if it's binary,

You guess right.  round() function is only theoretical, when applied to 
floats.  It can be useful when combined with formatting, as suggested 
elsewhere in the thread.  But even if .76 prints out "correctly," you 
have no assurance that adding two such apparently-exact figures will 
give one that will also print out easily.

> although Python must have a way to handle
> money amounts. I'm only on Chapter 2 ;')
>
> I assume Python has some automatic way to filter input, so that if
> someone entered three decimals instead of two for a money amount, they
> could get a wrist slap. Can you direct me to that functionality?
> Thanks.
>

There's no "money type" in Python, and if you want your user to get 
slapped, you have to write your own.  Normally, you take input in the 
form of text, validate it, then convert to int, float, Decimal, or 
whatever.  If the conversion itself will catch the errors, then you can 
just use try/catch to make such errors more polite to your users. 
That's the case if they enter  "charlie" when you're asking for a salary 
value.  But if they were to put  7.500 when the value is supposed to be 
in dollars and cents, then by the time it's turned into a float or 
decimal, the extra zero is long gone.

-- 
DaveA


More information about the Tutor mailing list