Intermediate · Functions · 115 of 264
Temperature converter
Write two functions, c_to_f and f_to_c, and convert a temperature that comes with its unit. F = C * 9 / 5 + 32.
Input: a number and a unit letter, e.g. 100 C or 32 F. Output: the converted value with one decimal and the other unit, e.g. 212.0 F
Examples
Input
100 C
Output
212.0 F
Input
32 F
Output
0.0 C
Input
-40 C
Output
-40.0 F
One more test checks other inputs when you press Check.
Stuck? The idea behind functions
A function, made with def, gives a name to a piece of code, so it can be used again: its parameters are the values it is given, return hands an answer back.
A function that returns lets the caller decide what to do with the answer: print it, compare it, use it in a sum.
def is_even(n):
return n % 2 == 0
print(is_even(4))
print(is_even(7))Watch for: print inside a function is not return: the caller gets None. And the code after return in the same block never runs.