Unit Converter

Convert between different units.

BeginnerPhase 6 - Category 5: Real-World Logic ProblemsExample 15 of 15
unit-converter.py
1# Take inputs
2value = float(input("Enter value: "))
3from_unit = input("From unit (km/m, kg/g, C/F): ")
4to_unit = input("To unit (km/m, kg/g, C/F): ")
5
6# Convert
7if from_unit == 'km' and to_unit == 'm':
8 result = value * 1000
9elif from_unit == 'm' and to_unit == 'km':
10 result = value / 1000
11elif from_unit == 'kg' and to_unit == 'g':
12 result = value * 1000
13elif from_unit == 'g' and to_unit == 'kg':
14 result = value / 1000
15elif from_unit == 'C' and to_unit == 'F':
16 result = (value * 9/5) + 32
17elif from_unit == 'F' and to_unit == 'C':
18 result = (value - 32) * 5/9
19else:
20 result = "Invalid conversion"
21
22print(f"Result: {result}")

Output

Enter value: 5
From unit (km/m, kg/g, C/F): km
To unit (km/m, kg/g, C/F): m
Result: 5000.0

What's going on

Use conditionals for different conversions.

Key Concepts:

Check unit types
Apply conversion formula
Handle invalid cases