I was wondering if there's a way of compacting this code:
foo = calculate() # 'calculate' is some kind of function which
# could return float values less than 0.0 or more than 1.0
# It's here only as a (hopefully) clarification
bar = foo * 5
if bar < 0:
bar = 0
elif bar > 1:
bar = 1
else:
pass # 'bar' has a good value within the "range"
I know I could do bar = max(foo * 5, 0)
or bar = min(foo * 5, 1)
but... Is there a way of doing this in only one line? Something like bar = ensure_between(0, foo * 5, 1)
or something like that?
It's not a big deal, I'm just curious.
Thank you in advance :-)