Logic Building
# Take inputs
value = float(input("Enter value: "))
from_unit = input("From unit (km/m, kg/g, C/F): ")
to_unit = input("To unit (km/m, kg/g, C/F): ")
# Convert
if from_unit == 'km' and to_unit == 'm':
result = value * 1000
elif from_unit == 'm' and to_unit == 'km':
result = value / 1000
elif from_unit == 'kg' and to_unit == 'g':
result = value * 1000
elif from_unit == 'g' and to_unit == 'kg':
result = value / 1000
elif from_unit == 'C' and to_unit == 'F':
result = (value * 9/5) + 32
elif from_unit == 'F' and to_unit == 'C':
result = (value - 32) * 5/9
else:
result = "Invalid conversion"
print(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
Use conditionals for different conversions.
Key Concepts:
- Check unit types
- Apply conversion formula
- Handle invalid cases