from math import floor
from fractions import Fraction
def convert(n):
feet, r = int(floor(n) / 12), n % 12
inches = int(floor(r))
numer = int(round(32 * (r - inches)))
return feet, inches, Fraction(numer, 32)
def show_inches(inches, fractions):
def pluralize_inches(i):
if i == 1:
return str(i) + " inch"
else:
return str(i) + " inches"
if inches and fractions:
return str(inches) + ' and ' + str(fractions) + ' inches'
elif inches or fractions:
return pluralize_inches(inches or fractions)
else: return ''
def feet_and_inches(n):
def pluralize_feet(ft):
if not ft:
return ''
elif ft == 1:
return str(ft) + ' foot'
else:
return str(ft) + ' feet'
if not n:
return "0 feet 0 inches"
else:
feet, inches, fractions = convert(n)
ft = pluralize_feet(feet)
i = show_inches(inches, fractions)
return ft + (ft and i and ' ') + i