Challenge: Temperature Converter
Completed
Write two functions: c_to_f(c) returns c * 9 / 5 + 32 and f_to_c(f) returns (f - 32) * 5 / 9. Use them to convert a temperature.
Input
One line: a number followed by C or F, e.g. 100 C.
Output
The converted temperature with 1 decimal place and the other unit, e.g. 212.0 F.
Example
Input
100 C
Output
212.0 F
Editor
Python
Hints
split() gives the number and the unit.
Convert the number with float().
f"{x:.1f}" shows one decimal place.
Solution explanation
Each function does one conversion and returns the answer, so the main code just picks which one to call.
Show solution
def c_to_f(c):
return c * 9 / 5 + 32
def f_to_c(f):
return (f - 32) * 5 / 9
value, unit = input().split()
value = float(value)
if unit == "C":
print(f"{c_to_f(value):.1f} F")
else:
print(f"{f_to_c(value):.1f} C")