Computer Science
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.
Python
Python Libraries
4 Likes
Answer
# lengthconversion.py
"""Conversion functions for various lengths"""
#Constants
MILE_TO_KM = 1.609344
KM_TO_MILE = 1 / MILE_TO_KM
FEET_TO_INCHES = 12
INCHES_TO_FEET = 1 / FEET_TO_INCHES
#Functions
def miletokm(miles):
"""Returns: miles converted to kilometers"""
return miles * MILE_TO_KM
def kmtomile(kilometers):
"""Returns: kilometers converted to miles"""
return kilometers * KM_TO_MILE
def feettoinches(feet):
"""Returns: feet converted to inches"""
return feet * FEET_TO_INCHES
def inchestofeet(inches):
"""Returns: inches converted to feet"""
return inches * INCHES_TO_FEET
Output
5 miles to kilometers = 8.04672 km
5 kilometers to miles = 3.1068559611866697 miles
5 feet to inches = 60 inches
24 inches to feet = 2.0 feet
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 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.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.