3.2. Temperature Conversion ¶
For this task, you need to write a function named convert
that
converts temperature values in Fahrenheit to Celcius and vice versa. The
function will take 2 arguments, an integer and a one-character string.
The integer will denote the temperature and the string will denote the
unit of temperature. (‘F’ means that the given temperature is in
Fahrenheit and ‘C’ means that it is in Celcius). If the unit is not ‘F’
or ‘C’, you should simply return “invalid unit”. If the given unit is
valid, then the function must return a float as the result of
conversion.
Hint: You can use the formula \(\frac{C}{100} = \frac{F-32}{180}\). In this formula, \(C\) is the value in Celcius and \(F\) is the value in Fahrenheit.
Sample I/O:
>>> convert(-35, 'C')
-31.0
>>> convert(212, 'F')
100.0
def convert(temperature, unit):
if(unit == 'C'):
new_temperature = (180/100) * temperature + 32
return new_temperature
elif(unit == 'F'):
new_temperature = (100/180) * (temperature - 32)
return new_temperature
else:
return "invalid unit"