Computer Science
Create a module MassConversion.py that stores function for mass conversion e.g.,
1.kgtotonne() — to convert kg to tonnes
- tonnetokg() — to convert tonne to kg
- kgtopound() — to convert kg to pound
- 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
What are the possible outcome(s) executed from the following code ? Also specify the maximum and minimum values that can be assigned to variable PICK.
import random PICK = random.randint(0, 3) CITY = ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"]; for I in CITY : for J in range(1, PICK): print(I, end = " ") print()
- DELHIDELHI
MUMBAIMUMBAI
CHENNAICHENNAI
KOLKATAKOLKATA - DELHI
DELHIMUMBAI
DELHIMUMBAICHENNAI - DELHI
MUMBAI
CHENNAI
KOLKATA - DELHI
MUMBAIMUMBAI
KOLKATAKOLKATAKOLKATA
- DELHIDELHI
Write a Python program having following functions :
- A function with the following signature :
remove_letter(sentence, letter)
This function should take a string and a letter (as a single-character string) as arguments, returning a copy of that string with every instance of the indicated letter removed. For example, remove_letter("Hello there!", "e") should return the string "Hllo thr!".
Try implementing it using<str>.split()
function. - Write a function to do the following :
Try implementing the capwords() functionality using other functions, i.e., split(), capitalize() and join(). Compare the result with the capwords() function's result.
- A function with the following signature :
Create a module lengthconversion.py that stores functions for various lengths conversion e.g.,
- miletokm() — to convert miles to kilometer
- kmtomile() — to convert kilometers to miles
- feettoinches()
- inchestofeet()
It should also store constant values such as value of (mile in kilometers and vice versa).
[1 mile = 1.609344 kilometer ; 1 feet = 12 inches]
Help() function should display proper information.
Create a package from above two modules as this :
Conversion ├──Length │ └──Lengthconversion.py └──Mass └──Massconversion. py
Make sure that above package meets the requirements of being a Python package. Also, you should be able to import above package and/or its modules using import command.