Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Last active October 19, 2020 01:04
Show Gist options
  • Save ZechCodes/5ddce5b87f321154535f4289e20b6b10 to your computer and use it in GitHub Desktop.
Save ZechCodes/5ddce5b87f321154535f4289e20b6b10 to your computer and use it in GitHub Desktop.
Challenge 135 - Temperature Conversion

Challenge 135 - Temperature Conversion

Write a program that takes a temperature input in celsius and converts it to Fahrenheit and Kelvin. Return the converted temperature values in a tuple.

The formula to calculate the temperature in Fahrenheit from Celsius is:

F = C*9/5 +32

The formula to calculate the temperature in Kelvin from Celsius is:

K = C + 273.15

Examples

temp_conversion(0) ➞ (32, 273.15)
# 0°C is equal to 32°F and 273.15 K.

temp_conversion(100) ➞ (212, 373.15)

temp_conversion(-10) ➞ (14, 263.15)

temp_conversion(300.4) ➞ (572.72, 573.55)

Notes

  • Return calculated temperatures up to two decimal places.
  • Return "Invalid" if K is less than 0.
from __future__ import annotations
from typing import Union
import unittest
def temp_conversion(celsius: float) -> Union[tuple[float, float], str]:
return "" # Put your code here!!!
class Test(unittest.TestCase):
def test_1(self):
self.assertEqual((32, 273.15), temp_conversion(0))
def test_2(self):
self.assertEqual((212, 373.15), temp_conversion(100))
def test_3(self):
self.assertEqual((109.04, 315.95), temp_conversion(42.8))
def test_4(self):
self.assertEqual((572.72, 573.55), temp_conversion(300.4))
def test_5(self):
self.assertEqual((12.74, 262.45), temp_conversion(-10.7))
def test_6(self):
self.assertEqual((-459.63, 0.02), temp_conversion(-273.13))
def test_7(self):
self.assertEqual("Invalid", temp_conversion(-273.16))
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment