conversion of length units available units metre kilometre megametre gigametre terametre petametre exametre zettametre yottametre usage import this file into their respective project use the function lengthconversion for conversion of length units parameters value the number of from units you want to convert fromtype from which type you want to convert totype to which type you want to convert references wikipedia reference https en wikipedia orgwikimeter wikipedia reference https en wikipedia orgwikikilometer wikipedia reference https en wikipedia orgwikiordersofmagnitudelength exponent of the factormeter conversion between astronomical length units lengthconversion1 meter kilometer 0 001 lengthconversion1 meter megametre 1e06 lengthconversion1 gigametre meter 1000000000 lengthconversion1 gigametre terametre 0 001 lengthconversion1 petametre terametre 1000 lengthconversion1 petametre exametre 0 001 lengthconversion1 terametre zettametre 1e09 lengthconversion1 yottametre zettametre 1000 lengthconversion4 wrongunit inch traceback most recent call last valueerror invalid fromtype value wrongunit conversion abbreviations are m km mm gm tm pm em zm ym exponent of the factor meter conversion between astronomical length units length_conversion 1 meter kilometer 0 001 length_conversion 1 meter megametre 1e 06 length_conversion 1 gigametre meter 1000000000 length_conversion 1 gigametre terametre 0 001 length_conversion 1 petametre terametre 1000 length_conversion 1 petametre exametre 0 001 length_conversion 1 terametre zettametre 1e 09 length_conversion 1 yottametre zettametre 1000 length_conversion 4 wrongunit inch traceback most recent call last valueerror invalid from_type value wrongunit conversion abbreviations are m km mm gm tm pm em zm ym
UNIT_SYMBOL = {
    "meter": "m",
    "kilometer": "km",
    "megametre": "Mm",
    "gigametre": "Gm",
    "terametre": "Tm",
    "petametre": "Pm",
    "exametre": "Em",
    "zettametre": "Zm",
    "yottametre": "Ym",
}

METRIC_CONVERSION = {
    "m": 0,
    "km": 3,
    "Mm": 6,
    "Gm": 9,
    "Tm": 12,
    "Pm": 15,
    "Em": 18,
    "Zm": 21,
    "Ym": 24,
}


def length_conversion(value: float, from_type: str, to_type: str) -> float:
    

    from_sanitized = from_type.lower().strip("s")
    to_sanitized = to_type.lower().strip("s")

    from_sanitized = UNIT_SYMBOL.get(from_sanitized, from_sanitized)
    to_sanitized = UNIT_SYMBOL.get(to_sanitized, to_sanitized)

    if from_sanitized not in METRIC_CONVERSION:
        msg = (
            f"Invalid 'from_type' value: {from_type!r}.\n"
            f"Conversion abbreviations are: {', '.join(METRIC_CONVERSION)}"
        )
        raise ValueError(msg)
    if to_sanitized not in METRIC_CONVERSION:
        msg = (
            f"Invalid 'to_type' value: {to_type!r}.\n"
            f"Conversion abbreviations are: {', '.join(METRIC_CONVERSION)}"
        )
        raise ValueError(msg)
    from_exponent = METRIC_CONVERSION[from_sanitized]
    to_exponent = METRIC_CONVERSION[to_sanitized]
    exponent = 1

    if from_exponent > to_exponent:
        exponent = from_exponent - to_exponent
    else:
        exponent = -(to_exponent - from_exponent)

    return value * pow(10, exponent)


if __name__ == "__main__":
    from doctest import testmod

    testmod()