KnowledgeBoat Logo

Computer Science

Create a module MassConversion.py that stores function for mass conversion e.g.,
1.kgtotonne() — to convert kg to tonnes

  1. tonnetokg() — to convert tonne to kg
  2. kgtopound() — to convert kg to pound
  3. poundtokg() — to convert pound to kg

(Also store constants 1 kg = 0.001 tonne, 1 kg = 2.20462 pound)
Help( ) function should give proper information about the module.

Python

Python Libraries

4 Likes

Answer

# MassConversion.py
"""Conversion functions between different masses"""

#Constants
KG_TO_TONNE = 0.001
TONNE_TO_KG = 1 / KG_TO_TONNE
KG_TO_POUND = 2.20462
POUND_TO_KG = 1 / KG_TO_POUND

#Functions
def kgtotonne(kg):
    """Returns: kilogram converted to tonnes"""
    return kg * KG_TO_TONNE

def tonnetokg(tonne):
    """Returns: tonne converted to kilogram"""
    return tonne * TONNE_TO_KG

def kgtopound(kg):
    """Returns: kilogram converted to pound"""
    return kg * KG_TO_POUND

def poundtokg(pound):
    """Returns: pound converted to kilogram"""
    return pound * POUND_TO_KG

Output

6 kilograms to tonnes = 0.006 tonnes
8 tonnes to kilograms = 8000.0 kilograms
5 kilograms to pound = 11.0231 pounds
18 pounds to kilograms = 8.164672369841515 kilograms

Answered By

1 Like


Related Questions