From 1fb92e76981872f456a957c3fbb5b70c1ef7c8cc Mon Sep 17 00:00:00 2001 From: karimzakir Date: Wed, 8 Jul 2020 22:11:16 +0400 Subject: [PATCH 01/14] added conversions between celsius and fahrenheit --- conversions/celsius_fahrenheit_conversion.py | 34 ++++++++++++++++++ conversions/fahrenheit_to_celsius.py | 38 ++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 conversions/celsius_fahrenheit_conversion.py create mode 100644 conversions/fahrenheit_to_celsius.py diff --git a/conversions/celsius_fahrenheit_conversion.py b/conversions/celsius_fahrenheit_conversion.py new file mode 100644 index 000000000000..c4ab2f99b7a8 --- /dev/null +++ b/conversions/celsius_fahrenheit_conversion.py @@ -0,0 +1,34 @@ +""" Convert temparature from Celsius to Fahrenheit """ + +def celsius_to_fahrenheit(celsius): + """ + Convert a given value from Celsius to Fahrenheit + + >>> celsius_to_fahrenheit(-40) + -40.0 + >>> celsius_to_fahrenheit(-20) + -4.0 + >>> celsius_to_fahrenheit(0) + 32.0 + >>> celsius_to_fahrenheit(20) + 68.0 + >>> celsius_to_fahrenheit(40) + 104.0 + >>> celsius_to_fahrenheit("40") + Traceback (most recent call last): + ... + TypeError: 'str' object cannot be interpreted as integer + """ + + if type(celsius) == str: + """ + Check whether given value is string and raise Type Error + """ + raise TypeError("'str' object cannot be interpreted as integer") + + fahrenheit = (celsius * 9/5) + 32 # value converted from celsius to fahrenheit + print(fahrenheit) + +if __name__ == "__main__": + import doctest + doctest.testmod() diff --git a/conversions/fahrenheit_to_celsius.py b/conversions/fahrenheit_to_celsius.py new file mode 100644 index 000000000000..d988656e7688 --- /dev/null +++ b/conversions/fahrenheit_to_celsius.py @@ -0,0 +1,38 @@ +""" Convert temparature from Fahrenheit to Celsius """ + +def fahrenheit_to_celsius(fahrenheit): + """ + Convert a given value from Fahrenheit to Celsius and round it to 2 d.p. + + >>> fahrenheit_to_celsius(0) + -17.78 + >>> fahrenheit_to_celsius(20) + -6.67 + >>> fahrenheit_to_celsius(40) + 4.44 + >>> fahrenheit_to_celsius(60) + 15.56 + >>> fahrenheit_to_celsius(80) + 26.67 + >>> fahrenheit_to_celsius(100) + 37.78 + >>> fahrenheit_to_celsius("100") + Traceback (most recent call last): + ... + TypeError: 'str' object cannot be interpreted as integer + """ + if type(fahrenheit) == str: + """ + Check whether given value is string and raise Type Error + """ + raise TypeError("'str' object cannot be interpreted as integer") + + + celsius = (fahrenheit - 32)*5/9 # value converted from fahrenheit to celsius + celsius = round(celsius, 2) # converted (celsius) value is rounded to two decimal places + print(celsius) + +if __name__ == "__main__": + import doctest + doctest.testmod() + From e403c2046690e01d2796b9558f1fec741f30f907 Mon Sep 17 00:00:00 2001 From: karimzakir Date: Wed, 8 Jul 2020 22:27:16 +0400 Subject: [PATCH 02/14] Renamed celsius_to_fahrenheit.py --- ...{celsius_fahrenheit_conversion.py => celsius_to_fahrenheit.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename conversions/{celsius_fahrenheit_conversion.py => celsius_to_fahrenheit.py} (100%) diff --git a/conversions/celsius_fahrenheit_conversion.py b/conversions/celsius_to_fahrenheit.py similarity index 100% rename from conversions/celsius_fahrenheit_conversion.py rename to conversions/celsius_to_fahrenheit.py From 149604d6a546d895eeab953c23c6ebb956c939fa Mon Sep 17 00:00:00 2001 From: karimzakir Date: Wed, 8 Jul 2020 22:35:51 +0400 Subject: [PATCH 03/14] Fixed spelling issues --- conversions/celsius_to_fahrenheit.py | 2 +- conversions/fahrenheit_to_celsius.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/conversions/celsius_to_fahrenheit.py b/conversions/celsius_to_fahrenheit.py index c4ab2f99b7a8..af896af27685 100644 --- a/conversions/celsius_to_fahrenheit.py +++ b/conversions/celsius_to_fahrenheit.py @@ -1,4 +1,4 @@ -""" Convert temparature from Celsius to Fahrenheit """ +""" Convert temperature from Celsius to Fahrenheit """ def celsius_to_fahrenheit(celsius): """ diff --git a/conversions/fahrenheit_to_celsius.py b/conversions/fahrenheit_to_celsius.py index d988656e7688..b4ebb55af3c3 100644 --- a/conversions/fahrenheit_to_celsius.py +++ b/conversions/fahrenheit_to_celsius.py @@ -1,4 +1,4 @@ -""" Convert temparature from Fahrenheit to Celsius """ +""" Convert temperature from Fahrenheit to Celsius """ def fahrenheit_to_celsius(fahrenheit): """ From d966cd301db93ee4fb61ff603a939180da6e94ac Mon Sep 17 00:00:00 2001 From: karimzakir Date: Wed, 8 Jul 2020 22:54:24 +0400 Subject: [PATCH 04/14] modified file to fit the 88-character limit --- conversions/fahrenheit_to_celsius.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/conversions/fahrenheit_to_celsius.py b/conversions/fahrenheit_to_celsius.py index b4ebb55af3c3..660ba8948db4 100644 --- a/conversions/fahrenheit_to_celsius.py +++ b/conversions/fahrenheit_to_celsius.py @@ -29,7 +29,8 @@ def fahrenheit_to_celsius(fahrenheit): celsius = (fahrenheit - 32)*5/9 # value converted from fahrenheit to celsius - celsius = round(celsius, 2) # converted (celsius) value is rounded to two decimal places + celsius = round(celsius, 2) + # converted (celsius) value is rounded to two decimal places print(celsius) if __name__ == "__main__": From f6a119c4330b149e5db2f3c5c8da9d8ac59b83d3 Mon Sep 17 00:00:00 2001 From: karimzakir Date: Wed, 8 Jul 2020 23:09:06 +0400 Subject: [PATCH 05/14] added changes to pass the travis-ci test --- conversions/celsius_to_fahrenheit.py | 6 ++++-- conversions/fahrenheit_to_celsius.py | 10 +++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/conversions/celsius_to_fahrenheit.py b/conversions/celsius_to_fahrenheit.py index af896af27685..8d5e5817b819 100644 --- a/conversions/celsius_to_fahrenheit.py +++ b/conversions/celsius_to_fahrenheit.py @@ -1,5 +1,6 @@ """ Convert temperature from Celsius to Fahrenheit """ + def celsius_to_fahrenheit(celsius): """ Convert a given value from Celsius to Fahrenheit @@ -26,9 +27,10 @@ def celsius_to_fahrenheit(celsius): """ raise TypeError("'str' object cannot be interpreted as integer") - fahrenheit = (celsius * 9/5) + 32 # value converted from celsius to fahrenheit + fahrenheit = (celsius * 9 / 5) + 32 # value converted from celsius to fahrenheit print(fahrenheit) + if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod() \ No newline at end of file diff --git a/conversions/fahrenheit_to_celsius.py b/conversions/fahrenheit_to_celsius.py index 660ba8948db4..691ae66c0b8a 100644 --- a/conversions/fahrenheit_to_celsius.py +++ b/conversions/fahrenheit_to_celsius.py @@ -1,5 +1,6 @@ """ Convert temperature from Fahrenheit to Celsius """ + def fahrenheit_to_celsius(fahrenheit): """ Convert a given value from Fahrenheit to Celsius and round it to 2 d.p. @@ -27,13 +28,12 @@ def fahrenheit_to_celsius(fahrenheit): """ raise TypeError("'str' object cannot be interpreted as integer") - - celsius = (fahrenheit - 32)*5/9 # value converted from fahrenheit to celsius - celsius = round(celsius, 2) + celsius = (fahrenheit - 32) * 5 / 9 # value converted from fahrenheit to celsius + celsius = round(celsius, 2) # converted (celsius) value is rounded to two decimal places print(celsius) + if __name__ == "__main__": import doctest - doctest.testmod() - + doctest.testmod() \ No newline at end of file From 724668522c79e5b54e8ae1be4ddaf9e7977aafdb Mon Sep 17 00:00:00 2001 From: karimzakir Date: Wed, 8 Jul 2020 23:15:36 +0400 Subject: [PATCH 06/14] further changed the files to pass the travis-ci test --- conversions/celsius_to_fahrenheit.py | 4 ++-- conversions/fahrenheit_to_celsius.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/conversions/celsius_to_fahrenheit.py b/conversions/celsius_to_fahrenheit.py index 8d5e5817b819..176a3d381f19 100644 --- a/conversions/celsius_to_fahrenheit.py +++ b/conversions/celsius_to_fahrenheit.py @@ -3,7 +3,7 @@ def celsius_to_fahrenheit(celsius): """ - Convert a given value from Celsius to Fahrenheit + Convert a given value from Celsius to Fahrenheit >>> celsius_to_fahrenheit(-40) -40.0 @@ -33,4 +33,4 @@ def celsius_to_fahrenheit(celsius): if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() diff --git a/conversions/fahrenheit_to_celsius.py b/conversions/fahrenheit_to_celsius.py index 691ae66c0b8a..35d536435a31 100644 --- a/conversions/fahrenheit_to_celsius.py +++ b/conversions/fahrenheit_to_celsius.py @@ -26,7 +26,7 @@ def fahrenheit_to_celsius(fahrenheit): """ Check whether given value is string and raise Type Error """ - raise TypeError("'str' object cannot be interpreted as integer") + raise TypeError("'str' object cannot be interpreted as integer") celsius = (fahrenheit - 32) * 5 / 9 # value converted from fahrenheit to celsius celsius = round(celsius, 2) From 6bf3dca13e96d6b56d019ba7965cce0907818833 Mon Sep 17 00:00:00 2001 From: karimzakir Date: Wed, 8 Jul 2020 23:19:37 +0400 Subject: [PATCH 07/14] further changed the files to pass the travis-ci test --- conversions/fahrenheit_to_celsius.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/fahrenheit_to_celsius.py b/conversions/fahrenheit_to_celsius.py index 35d536435a31..a00e4bda8329 100644 --- a/conversions/fahrenheit_to_celsius.py +++ b/conversions/fahrenheit_to_celsius.py @@ -36,4 +36,4 @@ def fahrenheit_to_celsius(fahrenheit): if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From ff16a89bfe37f87625a3274a2f3aad160e6a4664 Mon Sep 17 00:00:00 2001 From: karimzakir02 <61005718+karimzakir02@users.noreply.github.com> Date: Thu, 9 Jul 2020 13:09:57 +0400 Subject: [PATCH 08/14] Shortened conversions/fahrenheit_to_celsius.py Co-authored-by: Christian Clauss --- conversions/fahrenheit_to_celsius.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/conversions/fahrenheit_to_celsius.py b/conversions/fahrenheit_to_celsius.py index a00e4bda8329..804c414e91d4 100644 --- a/conversions/fahrenheit_to_celsius.py +++ b/conversions/fahrenheit_to_celsius.py @@ -28,10 +28,7 @@ def fahrenheit_to_celsius(fahrenheit): """ raise TypeError("'str' object cannot be interpreted as integer") - celsius = (fahrenheit - 32) * 5 / 9 # value converted from fahrenheit to celsius - celsius = round(celsius, 2) - # converted (celsius) value is rounded to two decimal places - print(celsius) + return round((fahrenheit - 32) * 5 / 9, 2) if __name__ == "__main__": From abfa0d5d042da793e47b5fd3fa7ecf3f3633156e Mon Sep 17 00:00:00 2001 From: karimzakir02 <61005718+karimzakir02@users.noreply.github.com> Date: Thu, 9 Jul 2020 13:15:42 +0400 Subject: [PATCH 09/14] Type hints added to conversions/fahrenheit_to_celsius.py Co-authored-by: Christian Clauss --- conversions/fahrenheit_to_celsius.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/fahrenheit_to_celsius.py b/conversions/fahrenheit_to_celsius.py index 804c414e91d4..0cf555a0c28b 100644 --- a/conversions/fahrenheit_to_celsius.py +++ b/conversions/fahrenheit_to_celsius.py @@ -1,7 +1,7 @@ """ Convert temperature from Fahrenheit to Celsius """ -def fahrenheit_to_celsius(fahrenheit): +def fahrenheit_to_celsius(fahrenheit: float) -> float: """ Convert a given value from Fahrenheit to Celsius and round it to 2 d.p. From e1673ef51d836377907f00aba5e9ee6c0e9a8f88 Mon Sep 17 00:00:00 2001 From: karimzakir Date: Thu, 9 Jul 2020 13:19:56 +0400 Subject: [PATCH 10/14] changed the code to let the caller do the printing --- conversions/celsius_to_fahrenheit.py | 14 +++++++------- conversions/fahrenheit_to_celsius.py | 16 ++++++++-------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/conversions/celsius_to_fahrenheit.py b/conversions/celsius_to_fahrenheit.py index 176a3d381f19..6b4af2feacfa 100644 --- a/conversions/celsius_to_fahrenheit.py +++ b/conversions/celsius_to_fahrenheit.py @@ -5,17 +5,17 @@ def celsius_to_fahrenheit(celsius): """ Convert a given value from Celsius to Fahrenheit - >>> celsius_to_fahrenheit(-40) + >>> print(celsius_to_fahrenheit(-40)) -40.0 - >>> celsius_to_fahrenheit(-20) + >>> print(celsius_to_fahrenheit(-20)) -4.0 - >>> celsius_to_fahrenheit(0) + >>> print(celsius_to_fahrenheit(0)) 32.0 - >>> celsius_to_fahrenheit(20) + >>> print(celsius_to_fahrenheit(20)) 68.0 - >>> celsius_to_fahrenheit(40) + >>> print(celsius_to_fahrenheit(40)) 104.0 - >>> celsius_to_fahrenheit("40") + >>> print(celsius_to_fahrenheit("40")) Traceback (most recent call last): ... TypeError: 'str' object cannot be interpreted as integer @@ -28,7 +28,7 @@ def celsius_to_fahrenheit(celsius): raise TypeError("'str' object cannot be interpreted as integer") fahrenheit = (celsius * 9 / 5) + 32 # value converted from celsius to fahrenheit - print(fahrenheit) + return fahrenheit if __name__ == "__main__": diff --git a/conversions/fahrenheit_to_celsius.py b/conversions/fahrenheit_to_celsius.py index 804c414e91d4..5df5e135db05 100644 --- a/conversions/fahrenheit_to_celsius.py +++ b/conversions/fahrenheit_to_celsius.py @@ -3,21 +3,21 @@ def fahrenheit_to_celsius(fahrenheit): """ - Convert a given value from Fahrenheit to Celsius and round it to 2 d.p. + Convert a given value from Fahrenheit to Celsius and round it to 2 decimal places. - >>> fahrenheit_to_celsius(0) + >>> print(fahrenheit_to_celsius(0)) -17.78 - >>> fahrenheit_to_celsius(20) + >>> print(fahrenheit_to_celsius(20)) -6.67 - >>> fahrenheit_to_celsius(40) + >>> print(fahrenheit_to_celsius(40)) 4.44 - >>> fahrenheit_to_celsius(60) + >>> print(fahrenheit_to_celsius(60)) 15.56 - >>> fahrenheit_to_celsius(80) + >>> print(fahrenheit_to_celsius(80)) 26.67 - >>> fahrenheit_to_celsius(100) + >>> print(fahrenheit_to_celsius(100)) 37.78 - >>> fahrenheit_to_celsius("100") + >>> print(fahrenheit_to_celsius("100")) Traceback (most recent call last): ... TypeError: 'str' object cannot be interpreted as integer From e8ee42d50ce67c8c0fbdf4b217c20a571e29f280 Mon Sep 17 00:00:00 2001 From: karimzakir Date: Thu, 9 Jul 2020 14:06:52 +0400 Subject: [PATCH 11/14] addressed the changes made on github --- conversions/celsius_to_fahrenheit.py | 18 ++++++------------ conversions/fahrenheit_to_celsius.py | 11 +++-------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/conversions/celsius_to_fahrenheit.py b/conversions/celsius_to_fahrenheit.py index 6b4af2feacfa..19fa67763c52 100644 --- a/conversions/celsius_to_fahrenheit.py +++ b/conversions/celsius_to_fahrenheit.py @@ -1,9 +1,9 @@ """ Convert temperature from Celsius to Fahrenheit """ -def celsius_to_fahrenheit(celsius): +def celsius_to_fahrenheit(celsius: float) -> float: """ - Convert a given value from Celsius to Fahrenheit + Convert a given value from Celsius to Fahrenheit and round it to 2 decimal places. >>> print(celsius_to_fahrenheit(-40)) -40.0 @@ -15,20 +15,14 @@ def celsius_to_fahrenheit(celsius): 68.0 >>> print(celsius_to_fahrenheit(40)) 104.0 - >>> print(celsius_to_fahrenheit("40")) + >>> print(celsius_to_fahrenheit("celsius")) Traceback (most recent call last): ... - TypeError: 'str' object cannot be interpreted as integer + ValueError: could not convert string to float: 'celsius' """ - if type(celsius) == str: - """ - Check whether given value is string and raise Type Error - """ - raise TypeError("'str' object cannot be interpreted as integer") - - fahrenheit = (celsius * 9 / 5) + 32 # value converted from celsius to fahrenheit - return fahrenheit + celsius = float(celsius) + return round((celsius * 9 / 5) + 32, 2) if __name__ == "__main__": diff --git a/conversions/fahrenheit_to_celsius.py b/conversions/fahrenheit_to_celsius.py index 4cba4377a878..20478084e5d2 100644 --- a/conversions/fahrenheit_to_celsius.py +++ b/conversions/fahrenheit_to_celsius.py @@ -17,17 +17,12 @@ def fahrenheit_to_celsius(fahrenheit: float) -> float: 26.67 >>> print(fahrenheit_to_celsius(100)) 37.78 - >>> print(fahrenheit_to_celsius("100")) + >>> print(fahrenheit_to_celsius("fahrenheit")) Traceback (most recent call last): ... - TypeError: 'str' object cannot be interpreted as integer + ValueError: could not convert string to float: 'fahrenheit' """ - if type(fahrenheit) == str: - """ - Check whether given value is string and raise Type Error - """ - raise TypeError("'str' object cannot be interpreted as integer") - + fahrenheit = float(fahrenheit) return round((fahrenheit - 32) * 5 / 9, 2) From d23994758b9ccb6847ef5ece6213f23189a2d22f Mon Sep 17 00:00:00 2001 From: Karim Zakir Date: Tue, 14 Jul 2020 13:21:01 +0300 Subject: [PATCH 12/14] Added Kelvin conversions and put temperature functions in a single file --- conversions/celsius_to_fahrenheit.py | 30 --------- conversions/fahrenheit_to_celsius.py | 31 --------- conversions/temperature_conversions.py | 92 ++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 61 deletions(-) delete mode 100644 conversions/celsius_to_fahrenheit.py delete mode 100644 conversions/fahrenheit_to_celsius.py create mode 100644 conversions/temperature_conversions.py diff --git a/conversions/celsius_to_fahrenheit.py b/conversions/celsius_to_fahrenheit.py deleted file mode 100644 index 19fa67763c52..000000000000 --- a/conversions/celsius_to_fahrenheit.py +++ /dev/null @@ -1,30 +0,0 @@ -""" Convert temperature from Celsius to Fahrenheit """ - - -def celsius_to_fahrenheit(celsius: float) -> float: - """ - Convert a given value from Celsius to Fahrenheit and round it to 2 decimal places. - - >>> print(celsius_to_fahrenheit(-40)) - -40.0 - >>> print(celsius_to_fahrenheit(-20)) - -4.0 - >>> print(celsius_to_fahrenheit(0)) - 32.0 - >>> print(celsius_to_fahrenheit(20)) - 68.0 - >>> print(celsius_to_fahrenheit(40)) - 104.0 - >>> print(celsius_to_fahrenheit("celsius")) - Traceback (most recent call last): - ... - ValueError: could not convert string to float: 'celsius' - """ - - celsius = float(celsius) - return round((celsius * 9 / 5) + 32, 2) - - -if __name__ == "__main__": - import doctest - doctest.testmod() diff --git a/conversions/fahrenheit_to_celsius.py b/conversions/fahrenheit_to_celsius.py deleted file mode 100644 index 20478084e5d2..000000000000 --- a/conversions/fahrenheit_to_celsius.py +++ /dev/null @@ -1,31 +0,0 @@ -""" Convert temperature from Fahrenheit to Celsius """ - - -def fahrenheit_to_celsius(fahrenheit: float) -> float: - """ - Convert a given value from Fahrenheit to Celsius and round it to 2 decimal places. - - >>> print(fahrenheit_to_celsius(0)) - -17.78 - >>> print(fahrenheit_to_celsius(20)) - -6.67 - >>> print(fahrenheit_to_celsius(40)) - 4.44 - >>> print(fahrenheit_to_celsius(60)) - 15.56 - >>> print(fahrenheit_to_celsius(80)) - 26.67 - >>> print(fahrenheit_to_celsius(100)) - 37.78 - >>> print(fahrenheit_to_celsius("fahrenheit")) - Traceback (most recent call last): - ... - ValueError: could not convert string to float: 'fahrenheit' - """ - fahrenheit = float(fahrenheit) - return round((fahrenheit - 32) * 5 / 9, 2) - - -if __name__ == "__main__": - import doctest - doctest.testmod() diff --git a/conversions/temperature_conversions.py b/conversions/temperature_conversions.py new file mode 100644 index 000000000000..563e6511d334 --- /dev/null +++ b/conversions/temperature_conversions.py @@ -0,0 +1,92 @@ +""" 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() From 2715614c4cde14c32d0b8dd18733fd6851fe2136 Mon Sep 17 00:00:00 2001 From: Karim Zakir Date: Tue, 14 Jul 2020 13:27:27 +0300 Subject: [PATCH 13/14] Removed whitespace from a blank line --- conversions/temperature_conversions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/temperature_conversions.py b/conversions/temperature_conversions.py index 563e6511d334..78763ef8a2d7 100644 --- a/conversions/temperature_conversions.py +++ b/conversions/temperature_conversions.py @@ -45,7 +45,7 @@ def fahrenheit_to_celsius(fahrenheit: float) -> float: ... ValueError: could not convert string to float: 'fahrenheit' """ - + return round((float(fahrenheit) - 32) * 5 / 9, 2) From 86eeead5cb8531ba4f46ce5aa855ed6578e30e3e Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 15 Jul 2020 17:02:04 +0200 Subject: [PATCH 14/14] Update temperature_conversions.py --- conversions/temperature_conversions.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/conversions/temperature_conversions.py b/conversions/temperature_conversions.py index 78763ef8a2d7..6b99d7688e44 100644 --- a/conversions/temperature_conversions.py +++ b/conversions/temperature_conversions.py @@ -20,7 +20,6 @@ def celsius_to_fahrenheit(celsius: float) -> float: ... ValueError: could not convert string to float: 'celsius' """ - return round((float(celsius) * 9 / 5) + 32, 2) @@ -45,7 +44,6 @@ def fahrenheit_to_celsius(fahrenheit: float) -> float: ... ValueError: could not convert string to float: 'fahrenheit' """ - return round((float(fahrenheit) - 32) * 5 / 9, 2) @@ -64,7 +62,6 @@ def celsius_to_kelvin(celsius: float) -> float: ... ValueError: could not convert string to float: 'celsius' """ - return round(float(celsius) + 273.15, 2) @@ -83,7 +80,6 @@ def kelvin_to_celsius(kelvin: float) -> float: ... ValueError: could not convert string to float: 'kelvin' """ - return round(float(kelvin) - 273.15, 2)