{}
run-icon
main.py
def length_converter(): print("\nLength Converter") units = {"1": "meters", "2": "kilometers", "3": "miles", "4": "centimeters"} for key, value in units.items(): print(f"{key}. {value.capitalize()}") from_unit = input("Convert from (1-4): ") to_unit = input("Convert to (1-4): ") if from_unit in units and to_unit in units: value = float(input(f"Enter value in {units[from_unit]}: ")) conversion_factors = { "1": {"2": 0.001, "3": 0.000621371, "4": 100}, "2": {"1": 1000, "3": 0.621371, "4": 100000}, "3": {"1": 1609.34, "2": 1.60934, "4": 160934}, "4": {"1": 0.01, "2": 0.00001, "3": 0.0000062137} } converted_value = value * conversion_factors[from_unit].get(to_unit, 1) print(f"{value} {units[from_unit]} = {converted_value} {units[to_unit]}") else: print("Invalid choice!") def temperature_converter(): print("\nTemperature Converter") print("1. Celsius\n2. Fahrenheit\n3. Kelvin") from_unit = input("Convert from (1-3): ") to_unit = input("Convert to (1-3): ") if from_unit in ["1", "2", "3"] and to_unit in ["1", "2", "3"]: value = float(input("Enter temperature: ")) conversions = { ("1", "2"): lambda x: (x * 9/5) + 32, ("1", "3"): lambda x: x + 273.15, ("2", "1"): lambda x: (x - 32) * 5/9, ("2", "3"): lambda x: (x - 32) * 5/9 + 273.15, ("3", "1"): lambda x: x - 273.15, ("3", "2"): lambda x: (x - 273.15) * 9/5 + 32 } converted_value = conversions.get((from_unit, to_unit), lambda x: x)(value) print(f"Converted Value: {converted_value:.2f}") else: print("Invalid choice!") while True: print("\nUnit Converter") print("1. Length Converter") print("2. Temperature Converter") print("3. Exit") choice = input("Choose an option (1-3): ") if choice == "1": length_converter() elif choice == "2": temperature_converter() elif choice == "3": print("Goodbye!") break else: print("Invalid choice, try again.")
Output