(Very Newbie) Problems defining a variable

Chris Rebert clp at rebertia.com
Fri Dec 12 13:11:30 CET 2008


On Fri, Dec 12, 2008 at 3:42 AM,  <febaen at gmail.com> wrote:
> #!/usr/bin/python
> #Py3k, UTF-8
>
<snip>
>
> #determine the interest rate to use
>        if bank >= 9999:
>                rate = 0.006
>        elif bank >= 10000 and bank <= 24999:
>                rate = 0.0085
>        elif bank >= 25000 and bank <= 49999:
>                rate = 0.0124
>        elif bank >= 50000 and bank <= 99999:
>                rate = 0.0149
>        elif bank >= 100000:
>                rate = 0.0173

For the love of Benji, reverse the ordering of the clauses so you
don't have to keep checking whether the number is also under the next
limit!
(I'm assuming Bruno's guess about your first test having the operator
flipped around the wrong way was right)

if bank >= 100000:
    rate = 0.0173
elif bank >= 50000:
    rate = 0.0149
elif bank >= 25000:
    rate = 0.0124
elif bank >= 10000:
    rate = 0.0085
else:
    rate = 0.006

Note how much simpler that is to read and understand. And switching
the default case to the 'else' is just idiomatic.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list