(Very Newbie) Problems defining a variable

Kirk Strauser kirk at daycos.com
Fri Dec 12 13:18:40 EST 2008


At 2008-12-12T18:12:39Z, "Tim Rowe" <digitig at gmail.com> writes:

> Is there a tidy way of making rates and thresholds local to get_rate,
> without recalculating each time? I suppose going object oriented is
> the proper way.
>
> #Py3k,UTF-8
>
> rates = {0: 0.006, 10000: 0.0085, 25000: 0.0124, 50000: 0.0149, 100000: 0.0173}
> thresholds = list(rates.keys())
> thresholds.sort()
> thresholds.reverse()
>
> def get_rate(balance):
>     for threshold in thresholds:
>         if balance >= threshold:
>             return rates[threshold]
>     else:
>         return 0.0

How 'bout:

def get_rate(balance):
    for threshold, rate in ((100000, .0173),
                            (50000, .0149),
                            (25000, .0124),
                            (10000, .0085),
                            (0, .006)):
        if balance > threshold:
            return rate
    return .0
-- 
Kirk Strauser
The Day Companies



More information about the Python-list mailing list