Skip to main content

Command Palette

Search for a command to run...

9. Unit Converter +GUI

Published
•2 min read•View as Markdown
9. Unit Converter +GUI

Today I realized that coding doesn’t have to be as complicated as I thought. Originally, I planned to write out every possible conversion for my unit converter manually — it would’ve been long and messy.

if choice == "weight"
    if choice1 == "1"
        weight = input("...

Then I discovered using a function with a dictionary to handle conversions dynamically. Suddenly, the code was shorter, cleaner, and easier to expand.

I even converted it into a GUI (using AI), so I can select categories and units from dropdowns and see results instantly. This taught me an important lesson: often, the smarter approach is to generalize with functions, making code simpler, flexible, and more enjoyable to use.

def weight_convert(weight, fromx, tox):   # kilogram
    conversion = {
    "kg": 1,
    "g": 0.001,     
    "oz": 0.0283495, 
    "lb": 0.453592  
    }


    return weight * conversion[fromx] / conversion[tox]

def length_convert(length, fromx, tox):
    conversion = {
    "m": 1,
    "cm": 0.01,   
    "mm": 0.001,  
    "km": 1000,    
    "inch": 0.0254,  
    "ft": 0.3048,   
    "yd": 0.9144, 
    "mile": 1609.34    
    }


    return length * conversion[fromx] / conversion[tox]

def vol_convert(volume, fromx, tox):
    conversion = {
    "l": 1,
    "ml": 0.001,       
    "m3": 1000,
    "cup": 0.24,    
    "pint": 0.473,  
    "quart": 0.946, 
    "gallon": 3.785 
    }


    return volume * conversion[fromx] / conversion[tox]





print("===== UNIT CONVERTER =====")
print("========== MENU ==========")
print("[weight:length:volume] (enter q to exit)")
option = input("[option]: ").lower()
print("==========================")

while True:
    if option == "weight":
        print("[kg] [g] [oz] [lb] (type q to exit)")
        fromx = input("[input]: ").lower()
        if fromx == "q":
            break
        tox = input("[output]: ").lower()
        if tox == "q":
            break

        weight = float(input(f"enter value in {fromx}: "))
        ans = weight_convert(weight,fromx,tox)
        print("===========================")
        print(f"{weight} {fromx} = {ans:.3f} {tox}")
        print("===========================")

    elif option == "length":
        print("[inch] [m] [yard] [km] [mile] (type q to exit)")
        fromx = input("[input]: ").lower()
        if fromx == "q":
            break
        tox = input("[output]: ").lower()
        if tox == "q":
            break

        length = float(input(f"enter value in {fromx}: "))
        ans = length_convert(length,fromx,tox)
        print("===========================")
        print(f"{length} {fromx} = {ans:.3f} {tox}")
        print("===========================")

    elif option == "volume":
        print("[l] [ml] [m3] [cup] [pint] [quart] [gallon] (type q to exit)")

        fromx = input("[input]: ").lower()
        if fromx == "q":
            break
        tox = input("[output]: ").lower()
        if tox == "q":
            break

        volume = float(input(f"enter value in {fromx}: "))
        ans = vol_convert(volume,fromx,tox)
        print("===========================")
        print(f"{volume} {fromx} = {ans:.3f} {tox}")
        print("===========================")

    elif option == "q":
        break

    else:
        print("invalid option. try again!")