So I currently have a function that outputs sound barrier for the input height.
def soundbarier (y):
if 0 < y < 1524:
S = 340.3
elif 1524 < y < 3048:
S = 334.4
elif 3048 < y < 4572:
S = 328.4
elif 4572 < y < 6096:
S = 322.2
elif 6096 < y < 7620:
S = 316.0
elif 7620 < y < 9144:
S = 309.6
elif 9144 < y < 10668:
S = 303.6
elif 10668 < y < 12192:
S = 295.4
else:
S = 294.5
return (float(S))
I want to shorten it using a dictionary but I can't get it to work.
def SOUND(y):
return {
0 < float(y) < 1524: 340.3,
1524 < y < 3048: 334.4,
3048 < y < 4572: 328.4,
4572 < y < 6096: 322.2,
6096 < y < 7620: 316.0,
7620 < y < 9144: 309.6,
9144 < y < 10668: 303.6,
10668 < y < 12192: 295.4
}.get(y, 9)
print(SOUND(1500))
I don't really want any default values. How could I make it work? I basically need the function to output S for a certain Y within a range.
Mathemagic by @Tim.
consts = [340.3, 334.4, 328.4, 322.2, 316.0, 309.6, 303.6, 295.4, 295.5]
def soundbarrier(y):
return consts[
-1 if (not (0 < y < 12192) or y % 1524 == 0) else y // 1524
]
In [1]: soundbarrier(1500)
Out[1]: 340.3
In [2]: soundbarrier(10000)
Out[2]: 303.6
In [3]: soundbarrier(100)
Out[3]: 340.3
In [4]: soundbarrier(1524)
Out[4]: 295.5
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