MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / volume_conversion

Function volume_conversion

conversions/volume_conversions.py:40–77  ·  view source on GitHub ↗

Conversion between volume units. >>> volume_conversion(4, "cubic meter", "litre") 4000 >>> volume_conversion(1, "litre", "gallon") 0.264172 >>> volume_conversion(1, "kilolitre", "cubic meter") 1 >>> volume_conversion(3, "gallon", "cubic yard") 0.01781427

(value: float, from_type: str, to_type: str)

Source from the content-addressed store, hash-verified

38
39
40def volume_conversion(value: float, from_type: str, to_type: str) -> float:
41 """
42 Conversion between volume units.
43 >>> volume_conversion(4, "cubic meter", "litre")
44 4000
45 >>> volume_conversion(1, "litre", "gallon")
46 0.264172
47 >>> volume_conversion(1, "kilolitre", "cubic meter")
48 1
49 >>> volume_conversion(3, "gallon", "cubic yard")
50 0.017814279
51 >>> volume_conversion(2, "cubic yard", "litre")
52 1529.1
53 >>> volume_conversion(4, "cubic foot", "cup")
54 473.396
55 >>> volume_conversion(1, "cup", "kilolitre")
56 0.000236588
57 >>> volume_conversion(4, "wrongUnit", "litre")
58 Traceback (most recent call last):
59 ...
60 ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are:
61 cubic meter, litre, kilolitre, gallon, cubic yard, cubic foot, cup
62 """
63 if from_type not in METRIC_CONVERSION:
64 raise ValueError(
65 f"Invalid 'from_type' value: {from_type!r} Supported values are:\n"
66 + ", ".join(METRIC_CONVERSION)
67 )
68 if to_type not in METRIC_CONVERSION:
69 raise ValueError(
70 f"Invalid 'to_type' value: {to_type!r}. Supported values are:\n"
71 + ", ".join(METRIC_CONVERSION)
72 )
73 return (
74 value
75 * METRIC_CONVERSION[from_type].from_factor
76 * METRIC_CONVERSION[to_type].to_factor
77 )
78
79
80if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected