return - Get a value between min and max values in Python -
i wan't value if it's between min , max values. if value smaller min, want min value , if it's greater max want max. use code now, there inbuilt or smarter way 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)
>>> 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
Comments
Post a Comment