I want to convert a float between 0.0 and 39.9 into a string. Replace the tens digit with an L, T or Y if it's a 1, 2 or 3 respectively. And append an M if it's in the ones. For example, 22.3 would return T2.3 and 8.1 would return M8.1 and so forth. Otherwise, return the float.
This code works of course, but I wondering if there was a simpler (if not one-liner) solution. Here's the code:
def specType(SpT):
if 0 <= SpT <= 9.9:
return 'M{}'.format(SpT)
elif 10.0 <= SpT <= 19.9:
return 'L{}'.format(SpT - 10)
elif 20.0 <= SpT <= 29.9:
return 'T{}'.format(SpT - 20)
elif 30.0 <= SpT <= 39.9:
return 'Y{}'.format(SpT - 30)
else:
return SpT
Thanks!
floatcomparisons (SpT <= 9.9etc)SpTis 9.95? Because it's greater than9.9and less than10.0, it falls through the cracks and reaches thereturn SpTline.