I would pick a standard unit of length, let's say m. Then I would have a single dictionary that gives a factor for each other unit, and convert:
conversion_factors = {
'foot': 0.3048, # Google search '1 foot in m'
'yard': 0.9144,
# etc
}
def unit_convert(number, from_unit='m', to_unit='m'):
m = number * conversion_factor[from_unit]
return m / conversion_factor[to_unit]
For the synonyms (feet, ft, etc) you could make a second dictionary and lookup the canonical name in that first:
conversion_factors = { ... } # as above
synonyms = {
'feet': 'foot',
'ft': 'foot',
...
}
def unit_convert(number, from_unit='m', to_unit='m'):
from_unit = synonyms.get(from_unit, from_unit)
to_unit = synonyms.get(to_unit, to_unit)
# etc
...or just put them in the conversion_factors
dictionary multiple times:
conversion_factors = {
'foot': 0.3048, # Google search '1 foot in m'
'feet': 0.3048,
'ft': 0.3048,
'yard': 0.9144,
# etc
}