Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Unit Converter

Convert selected length, mass, and temperature unit pairs. Students choose a valid branch and distinguish scale-only conversions from temperature offsets.

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

In this one

  • Match ordered unit pairs
  • Separate offsets from scaling
  • Reject unsupported pairs

How it works

  1. 1

    Read value and both unit labels.

  2. 2

    Choose one of the six supported conversion branches.

  3. 3

    Calculate its result or store Invalid conversion, then print.

Edge cases

Does km to kg work?

Does km to kg work?

No. It is not a supported conversion.

Does C to C return the original value?

Does C to C return the original value?

No. Same-unit cases are not implemented.

What's going on

Each branch matches both from_unit and to_unit. Length and mass pairs multiply or divide by one thousand; temperature pairs also account for the zero-point offset.

Only the listed pairs are supported. Labels are case-sensitive because no normalization occurs, and same-unit conversions are not included. The result is either a number or Invalid conversion. This is a small decision table, not a general dimensional-analysis library.

Does km to kg work? No. It is not a supported conversion.

Does C to C return the original value? No. Same-unit cases are not implemented.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why is Celsius-to-Fahrenheit not just scaling?

Pick an answer to check it.

Question 2

Does km to kg work?

Pick an answer to check it.

Questions people ask

Does km to kg work?

No. It is not a supported conversion.

Does C to C return the original value?

No. Same-unit cases are not implemented.

Why is Celsius-to-Fahrenheit not just scaling?

Its formula also adds 32.