Here’s a Python program to create a temperature converter that converts temperatures between Celsius, Fahrenheit, and Kelvin.
This program allows the user to input a temperature in one unit and convert it to another.
Code
# Conversion functions def celsius_to_fahrenheit(celsius): return (celsius * 9/5) + 32 def celsius_to_kelvin(celsius): return celsius + 273.15 def fahrenheit_to_celsius(fahrenheit): return (fahrenheit - 32) * 5/9 def fahrenheit_to_kelvin(fahrenheit): return (fahrenheit - 32) * 5/9 + 273.15 def kelvin_to_celsius(kelvin): return kelvin - 273.15 def kelvin_to_fahrenheit(kelvin): return (kelvin - 273.15) * 9/5 + 32 # Dictionary to map choices to functions conversion_functions = { '1': ('Celsius to Fahrenheit', celsius_to_fahrenheit), '2': ('Celsius to Kelvin', celsius_to_kelvin), '3': ('Fahrenheit to Celsius', fahrenheit_to_celsius), '4': ('Fahrenheit to Kelvin', fahrenheit_to_kelvin), '5': ('Kelvin to Celsius', kelvin_to_celsius), '6': ('Kelvin to Fahrenheit', kelvin_to_fahrenheit), } # Display conversion options def display_menu(): print("Choose a temperature conversion type:") for key, (name, _) in conversion_functions.items(): print(f"{key}. {name}") # Perform the conversion def convert_temperature(): while True: display_menu() choice = input("Enter the conversion type (or 'q' to quit): ") if choice.lower() == 'q': print("Exiting the Temperature Converter.") break if choice in conversion_functions: unit_name, conversion_function = conversion_functions[choice] try: value = float(input(f"Enter the temperature to convert for {unit_name}: ")) result = conversion_function(value) print(f"Converted temperature: {result}\n") except ValueError: print("Please enter a valid number.\n") else: print("Invalid choice. Please select a valid option.\n") # Start the temperature converter convert_temperature()
Explanation of the Code
- Define Conversion Functions:
- celsius_to_fahrenheit(celsius): Converts Celsius to Fahrenheit using the formula (C * 9/5) + 32.
- celsius_to_kelvin(celsius): Converts Celsius to Kelvin using the formula C + 273.15.
- fahrenheit_to_celsius(fahrenheit): Converts Fahrenheit to Celsius using the formula (F – 32) * 5/9.
- fahrenheit_to_kelvin(fahrenheit): Converts Fahrenheit to Kelvin using the formula (F – 32) * 5/9 + 273.15.
- kelvin_to_celsius(kelvin): Converts Kelvin to Celsius using the formula K – 273.15.
- kelvin_to_fahrenheit(kelvin): Converts Kelvin to Fahrenheit using the formula (K – 273.15) * 9/5 + 32.
- Dictionary for Conversion Options:
- conversion_functions: This dictionary maps each conversion type (e.g., ‘1’ for Celsius to Fahrenheit) to a tuple containing the conversion name and the respective function.
- Display Menu (display_menu function):
- This function displays the available conversion options by iterating through conversion_functions and showing each option’s key and name.
- Conversion Logic (convert_temperature function):
- User Choice:
- Calls display_menu() to show options.
- Prompts the user to select a conversion type or enter ‘q’ to quit.
- Conversion Execution:
- If the choice is valid (in conversion_functions), it:
- Retrieves the unit_name and conversion_function.
- Prompts the user to input a temperature to convert.
- Calls the appropriate conversion function.
- Displays the result.
- If the choice is valid (in conversion_functions), it:
- Error Handling:
- Catches invalid inputs (non-numeric values) with ValueError and displays an error message.
- Invalid Choice Handling:
- If the choice is not in conversion_functions, it shows an invalid choice message.
- User Choice:
- Run the Converter:
- convert_temperature() starts the program, displaying the menu and performing conversions.
Sample Output
>>> %Run convert_temperature.py Choose a temperature conversion type: 1. Celsius to Fahrenheit 2. Celsius to Kelvin 3. Fahrenheit to Celsius 4. Fahrenheit to Kelvin 5. Kelvin to Celsius 6. Kelvin to Fahrenheit Enter the conversion type (or 'q' to quit): 1 Enter the temperature to convert for Celsius to Fahrenheit: 10 Converted temperature: 50.0 Choose a temperature conversion type: 1. Celsius to Fahrenheit 2. Celsius to Kelvin 3. Fahrenheit to Celsius 4. Fahrenheit to Kelvin 5. Kelvin to Celsius 6. Kelvin to Fahrenheit Enter the conversion type (or 'q' to quit): 4 Enter the temperature to convert for Fahrenheit to Kelvin: 100 Converted temperature: 310.92777777777775 Choose a temperature conversion type: 1. Celsius to Fahrenheit 2. Celsius to Kelvin 3. Fahrenheit to Celsius 4. Fahrenheit to Kelvin 5. Kelvin to Celsius 6. Kelvin to Fahrenheit Enter the conversion type (or 'q' to quit):
Important Notes
- Error Handling: Checks for valid input values to avoid crashes from non-numeric inputs.
- Modular Design: Each conversion type has a dedicated function, making it easy to expand or modify.
- Extensibility: The use of a dictionary for mapping functions allows easy addition of new conversion types.
Summary
This Python program provides a simple text-based temperature converter, converting between Celsius, Fahrenheit, and Kelvin.
The convert_temperature() function handles the user input, displays conversion options, and calls the respective function based on the user’s selection. This design makes it easy to use, modify, and expand.