I am trying to write a program where if I call
get_bin_pos(19.4) ==> output as 20
get_bin_pos(13.4) ==> output as 20
get_bin_pos(31.4) ==> output as 40
I wrote something like below, it looks bad, any suggestion in optimizing it ?
Switch statement for this would be more readable, but we don't have a switch in python. So any other way?
def get_bin_pos(self, value):
if value <= 10:
return 10
if value <= 20:
return 20
if value <= 30:
return 30
if value <= 40:
return 40
if value <= 50:
return 50
if value <= 60:
return 60
if value <= 70:
return 70
return (int(value / 10) + 1) * 10
perhaps? \$\endgroup\$return int((value + 10) / 10) * 10
(similar to @vnp's suggestion, but more intuitive in my view) \$\endgroup\$(int(10 / 10) + 1) * 10 == 20
... Maybe not the correct method. \$\endgroup\$