Categories: Python
Tags:

fahrenheit = (celsius * 9/5) + 32

celsius = (fahrenheit – 32) * 5/9

Kelvin = (fahrenheit – 32) * 5/9 + 273.15

def temperature_converter():
    print("Welcome to the Temperature Converter!")
    print("Choose the conversion you want:")
    print("1. Celsius to Fahrenheit")
    print("2. Fahrenheit to Celsius")
    print("3. Celsius to Kelvin")
    print("4. Kelvin to Celsius")
    print("5. Fahrenheit to Kelvin")
    print("6. Kelvin to Fahrenheit")
    
    try:
        choice = int(input("Enter your choice (1-6): "))
        
        if choice == 1:
            celsius = float(input("Enter temperature in Celsius: "))
            fahrenheit = (celsius * 9/5) + 32
            print(f"{celsius}°C is equal to {fahrenheit:.2f}°F.")
        elif choice == 2:
            fahrenheit = float(input("Enter temperature in Fahrenheit: "))
            celsius = (fahrenheit - 32) * 5/9
            print(f"{fahrenheit}°F is equal to {celsius:.2f}°C.")
        elif choice == 3:
            celsius = float(input("Enter temperature in Celsius: "))
            kelvin = celsius + 273.15
            print(f"{celsius}°C is equal to {kelvin:.2f}K.")
        elif choice == 4:
            kelvin = float(input("Enter temperature in Kelvin: "))
            celsius = kelvin - 273.15
            print(f"{kelvin}K is equal to {celsius:.2f}°C.")
        elif choice == 5:
            fahrenheit = float(input("Enter temperature in Fahrenheit: "))
            kelvin = (fahrenheit - 32) * 5/9 + 273.15
            print(f"{fahrenheit}°F is equal to {kelvin:.2f}K.")
        elif choice == 6:
            kelvin = float(input("Enter temperature in Kelvin: "))
            fahrenheit = (kelvin - 273.15) * 9/5 + 32
            print(f"{kelvin}K is equal to {fahrenheit:.2f}°F.")
        else:
            print("Invalid choice. Please run the program again and choose a number between 1 and 6.")
    except ValueError:
        print("Invalid input. Please enter numeric values only.")

# Run the converter
temperature_converter()

How it works:

  1. The program presents a menu of 6 options for temperature conversions.
  2. The user selects one of the options by entering a number (1–6).
  3. The program asks for the required input temperature and performs the conversion using the relevant formula.
  4. The result is displayed to the user.

Example Output:

Welcome to the Temperature Converter!
Choose the conversion you want:
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Celsius to Kelvin
4. Kelvin to Celsius
5. Fahrenheit to Kelvin
6. Kelvin to Fahrenheit
Enter your choice (1-6): 1
Enter temperature in Celsius: 25
25.0°C is equal to 77.00°F.