[Tutor] Setting thresholds in a compact way

boB Stepp robertvstepp at gmail.com
Wed Jan 1 14:21:28 EST 2020


Greetings!

On Wed, Jan 1, 2020 at 12:47 PM Robert Alexander <gogonegro at gmail.com> wrote:

> I am trying to setup a varying time.sleep value (in house) depending on a
> priorly determined threshold.
>
> If threshold <= .9 AND > .8 then sleep should be 6 hours
> if threshold <= 0.8 AND > .4 then sleep should be 4 hours
> if threshold <= .4 AND > .1 then sleep should be 3 hours
> if threshold <= .1 then sleep should be set to 1 hour

Python allows you to write a somewhat simpler if condition instead of
using "and":

if 0.8 < threshold <= 0.9:
    <your code block here>
etc.

Are you aware of Python's if - elif - else structure?  In your case it could be:

if 0.8 < threshold <= 0.9:
    <code block 1>
elif 0.4 < threshold <= 0.8:
    <code block 2>
elif 0.1 < threshold <= 0.4:
    <code block 3>
elif threshold <= 0.1:  # What about negative values of threshold?
    <code block 4>
else:
    <Throw an exception or something similar for all other possible values?>

I'll leave your other questions to someone else.

HAPPY NEW YEAR TO YOU AND YOURS!!!

-- 
boB


More information about the Tutor mailing list