I wan't to get a value if it's between min and max values. If the value is smaller than min, I want to get the min value and if it's greater than max I want to get the max. I use this code now, but is there an inbuilt or smarter way to do it?
def inBetween(minv, val, maxv):
if minv < val < maxv: return val
if minv > val: return minv
if maxv < val: return maxv
print inBetween(2,5,10)
Using min
, max
:
>>> def inbetween(minv, val, maxv):
... return min(maxv, max(minv, val))
...
>>> inbetween(2, 5, 10)
5
>>> inbetween(2, 1, 10)
2
>>> inbetween(2, 11, 10)
10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With