Unit Converter
Convert between different units.
BeginnerPhase 6 - Category 5: Real-World Logic ProblemsExample 15 of 15
unit-converter.py
1# Take inputs2value = 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): ")56# Convert7if from_unit == 'km' and to_unit == 'm':8 result = value * 10009elif from_unit == 'm' and to_unit == 'km':10 result = value / 100011elif from_unit == 'kg' and to_unit == 'g':12 result = value * 100013elif from_unit == 'g' and to_unit == 'kg':14 result = value / 100015elif from_unit == 'C' and to_unit == 'F':16 result = (value * 9/5) + 3217elif from_unit == 'F' and to_unit == 'C':18 result = (value - 32) * 5/919else:20 result = "Invalid conversion"2122print(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
🔗Related Content
- 📖
Phase 6 - Learn Concepts
Review Phase 6 concepts and explanations
- 📝
Phase 6 - Quiz
Test your Phase 6 understanding with quiz questions
- 💻
Phase 6 - All Practice Problems
Explore all practice problems for Phase 6
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum