I have this small snippet of python code that I wrote. It works, but I think there should be a more streamlined method to achieve the same results. I’m just not seeing it. Any ideas?
if tx_avt >= 100: tx = 1
elif tx_avt < 100 and tx_avt >= 50: tx = 2
elif tx_avt < 50 and tx_avt >= 25: tx = 3
elif tx_avt < 25 and tx_avt >= 12.5: tx = 4
else: tx = 5
You can change it to:
Explanation:
if tx_avt >= 100is not true, then you can deduce thattx_avt < 100must be true.tx_avt < 100” part in the check “elif tx_avt < 100 and tx_avt >= 50:“.The same logic cascades down & applies to the rest of the
elifcases.Related reading: Why Python Doesn’t Have a Switch Statement, and its Alternatives.