mercredi 7 juillet 2021

IF condition update values depending out or inside interval

I would like to check if there is a more efficient (o neat) to express the next condition:

  • I want to check if the value of the parameter 'a' is inside the interval. If it is in the interval, a remains the same, but...
  • if it is lower or higher than the minimum or maximum value of the interval, the parameter 'a' should be updated with the minimum or maximum values depending on the case. This code works perfectly fine:
a=1000
if 0<=a<=800:
    a=a
elif a>800:
    a=800
else:
    a=0
print('a is',a)

I have to repeat this kind of condition for a relatively large set of different parameters, so I was wondering if I can do this in a more efficient/neat way.

Aucun commentaire:

Enregistrer un commentaire