[Tutor] Some questions about my yen-USD.py
Kent Johnson
kent37 at tds.net
Fri Sep 8 14:04:14 CEST 2006
Dick Moores wrote:
> Yes, Danny, I've taken yours and Andrei's comment about again() to
> heart; I see my confusion. I've revised again() and main(). See my
> version 2: http://www.rcblue.com/Python/yen-USD-v2.txt Is this better?
Have you noticed the similarity between getRate() and getAmount()? Maybe
you could make a single getPositiveNumber() function that replaces both?
A better name for comma() might be commify() ?
confirmCurrency() doesn't just confirm, it also puts the currency into a
standard form. I would make that a responsibility of getYenOrUSD().
Others have commented on potential problems with 'if currency not in
"YENUSD":'. I would combine the test with the normalization. Here are
two ways to do it:
by testing each one:
currency = currency.upper() # string.upper() is deprecated
if currency in ['Y', 'YEN']:
return 'Yen'
if currency in ['U', 'USD']:
return 'USD'
with a dictionary:
currencies = { 'U' : 'USD', 'USD' : 'USD', 'Y' : 'Yen', 'YEN' : 'Yen' }
try:
return currencies[currency]
except KeyError:
continue
The dict method is more extensible, you could even create the dict
automatically from a list of the return codes you want.
Kent
More information about the Tutor
mailing list