mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-31 06:33:44 +00:00
Celsius to Fahrenheit Conversions (#2188)
* added conversions between celsius and fahrenheit * Renamed celsius_to_fahrenheit.py * Fixed spelling issues * modified file to fit the 88-character limit * added changes to pass the travis-ci test * further changed the files to pass the travis-ci test * further changed the files to pass the travis-ci test * Shortened conversions/fahrenheit_to_celsius.py Co-authored-by: Christian Clauss <cclauss@me.com> * Type hints added to conversions/fahrenheit_to_celsius.py Co-authored-by: Christian Clauss <cclauss@me.com> * changed the code to let the caller do the printing * addressed the changes made on github * Added Kelvin conversions and put temperature functions in a single file * Removed whitespace from a blank line * Update temperature_conversions.py Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
23cbe4c352
commit
88e82db89a
88
conversions/temperature_conversions.py
Normal file
88
conversions/temperature_conversions.py
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
""" Convert between different units of temperature """
|
||||||
|
|
||||||
|
|
||||||
|
def celsius_to_fahrenheit(celsius: float) -> float:
|
||||||
|
"""
|
||||||
|
Convert a given value from Celsius to Fahrenheit and round it to 2 decimal places.
|
||||||
|
|
||||||
|
>>> celsius_to_fahrenheit(-40.0)
|
||||||
|
-40.0
|
||||||
|
>>> celsius_to_fahrenheit(-20.0)
|
||||||
|
-4.0
|
||||||
|
>>> celsius_to_fahrenheit(0)
|
||||||
|
32.0
|
||||||
|
>>> celsius_to_fahrenheit(20)
|
||||||
|
68.0
|
||||||
|
>>> celsius_to_fahrenheit("40")
|
||||||
|
104.0
|
||||||
|
>>> celsius_to_fahrenheit("celsius")
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: could not convert string to float: 'celsius'
|
||||||
|
"""
|
||||||
|
return round((float(celsius) * 9 / 5) + 32, 2)
|
||||||
|
|
||||||
|
|
||||||
|
def fahrenheit_to_celsius(fahrenheit: float) -> float:
|
||||||
|
"""
|
||||||
|
Convert a given value from Fahrenheit to Celsius and round it to 2 decimal places.
|
||||||
|
|
||||||
|
>>> fahrenheit_to_celsius(0)
|
||||||
|
-17.78
|
||||||
|
>>> fahrenheit_to_celsius(20.0)
|
||||||
|
-6.67
|
||||||
|
>>> fahrenheit_to_celsius(40.0)
|
||||||
|
4.44
|
||||||
|
>>> fahrenheit_to_celsius(60)
|
||||||
|
15.56
|
||||||
|
>>> fahrenheit_to_celsius(80)
|
||||||
|
26.67
|
||||||
|
>>> fahrenheit_to_celsius("100")
|
||||||
|
37.78
|
||||||
|
>>> fahrenheit_to_celsius("fahrenheit")
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: could not convert string to float: 'fahrenheit'
|
||||||
|
"""
|
||||||
|
return round((float(fahrenheit) - 32) * 5 / 9, 2)
|
||||||
|
|
||||||
|
|
||||||
|
def celsius_to_kelvin(celsius: float) -> float:
|
||||||
|
"""
|
||||||
|
Convert a given value from Celsius to Kelvin and round it to 2 decimal places.
|
||||||
|
|
||||||
|
>>> celsius_to_kelvin(0)
|
||||||
|
273.15
|
||||||
|
>>> celsius_to_kelvin(20.0)
|
||||||
|
293.15
|
||||||
|
>>> celsius_to_kelvin("40")
|
||||||
|
313.15
|
||||||
|
>>> celsius_to_kelvin("celsius")
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: could not convert string to float: 'celsius'
|
||||||
|
"""
|
||||||
|
return round(float(celsius) + 273.15, 2)
|
||||||
|
|
||||||
|
|
||||||
|
def kelvin_to_celsius(kelvin: float) -> float:
|
||||||
|
"""
|
||||||
|
Convert a given value from Kelvin to Celsius and round it to 2 decimal places.
|
||||||
|
|
||||||
|
>>> kelvin_to_celsius(273.15)
|
||||||
|
0.0
|
||||||
|
>>> kelvin_to_celsius(300)
|
||||||
|
26.85
|
||||||
|
>>> kelvin_to_celsius("315.5")
|
||||||
|
42.35
|
||||||
|
>>> kelvin_to_celsius("kelvin")
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: could not convert string to float: 'kelvin'
|
||||||
|
"""
|
||||||
|
return round(float(kelvin) - 273.15, 2)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
Loading…
Reference in New Issue
Block a user