Solution 1

``````class NoTemperatureException(Exception):
pass

class TempConverter(object):
def __init__(self, fahrenheit=None, celsius=None):
if not fahrenheit and not celsius:
raise NoTemperatureException()
self.temp_fahrenheit = fahrenheit
self.temp_celsius = celsius

def to_celsius(self):
if self.temp_celsius:
return self.temp_celsius
return round((float(self.temp_fahrenheit) - 32) * 5/9, 1)

def to_fahrenheit(self):
if self.temp_fahrenheit:
return self.temp_fahrenheit
return round((float(self.temp_celsius) * 9/5) + 32, 1)``````

# Temperature Converter

Create a class `TempConverter` that has two optional parameters `celsius` and `fahrenheit`. When an object is created, if it receives neither of the optional parameters, raise a custom exception NoTemperatureException that you will need to create.

You'll need to differentiate which type of temperature you receive when you create your object. If the object receives a Celsius temp, store it in a variable `temp_celsius`. If it receives a Fahrenheit temp, store it in a variable `temp_fahrenheit`.

You need two methods:
`to_celsius` that returns the temperature in Celsius. If a Celsius input was received when the object was created, return that. Otherwise return the conversion formula (temp_fahrenheit - 32) * 5/9

`to_fahrenheit` that returns the temperature in Fahrenheit. If a Fahrenheit input was received when the object was created, return that. Otherwise return the conversion formula (temp_celsius * 9/5) + 32

Look up how to use the round() function on your answers to make them only have one decimal place to the right.

Examples:

``````t = TempConverter(fahrenheit=90)
assert t.to_celsius() == 32.2
assert t.to_fahrenheit() == 90

t = TempConverter(celsius=20)
assert t.to_celsius() == 20
assert t.to_fahrenheit() == 68
``````

### Test Cases

test no temperature -

``````import pytest

def test_no_temperature():
with pytest.raises(NoTemperatureException):
t = TempConverter()
``````

test celsius -

``````def test_celsius():
t = TempConverter(celsius=20)
assert t.to_celsius() == 20
assert t.to_fahrenheit() == 68``````

test fahrenheit -

``````def test_fahrenheit():
t = TempConverter(fahrenheit=90)
assert t.to_celsius() == 32.2
assert t.to_fahrenheit() == 90``````