Temperature Converter

Convert between Celsius, Fahrenheit, and Kelvin.

IntermediatePhase 6 - Category 5: Real-World Logic ProblemsExample 6 of 15
temperature-converter.py
1# Take input
2temp = float(input("Enter temperature: "))
3unit = input("Enter unit (C/F/K): ").upper()
4
5# Convert to all units
6if unit == 'C':
7 celsius = temp
8 fahrenheit = (celsius * 9/5) + 32
9 kelvin = celsius + 273.15
10elif unit == 'F':
11 fahrenheit = temp
12 celsius = (fahrenheit - 32) * 5/9
13 kelvin = celsius + 273.15
14elif unit == 'K':
15 kelvin = temp
16 celsius = kelvin - 273.15
17 fahrenheit = (celsius * 9/5) + 32
18else:
19 print("Invalid unit")
20 exit()
21
22print(f"Celsius: {celsius:.2f}°C")
23print(f"Fahrenheit: {fahrenheit:.2f}°F")
24print(f"Kelvin: {kelvin:.2f}K")

Output

Enter temperature: 25
Enter unit (C/F/K): C
Celsius: 25.00°C
Fahrenheit: 77.00°F
Kelvin: 298.15K

What's going on

Convert between temperature scales.

Key Concepts:

F = (C * 9/5) + 32
K = C + 273.15
Convert based on input unit