2.0 KiB
2.0 KiB
| title | description | published | date | tags | editor | dateCreated |
|---|---|---|---|---|---|---|
| Temperature Converter | true | 2025-06-28T01:36:15.092Z | markdown | 2025-06-28T01:36:15.092Z |
Python Programming Exercise: Temperature Converter
Objective
Create a Python program that converts temperatures between Celsius, Fahrenheit, and Kelvin using functions. This exercise will help you practice defining and using functions, handling user input, and performing calculations.
Instructions
Write a Python program that:
- Defines three functions to handle temperature conversions:
celsius_to_fahrenheit(celsius): Converts Celsius to Fahrenheitcelsius_to_kelvin(celsius): Converts Celsius to Kelvinconvert_temperature(value, unit): Takes a temperature value and unit (C, F, or K) and returns conversions to the other two units
- Prompts the user to input a temperature value and its unit.
- Validates the input to ensure it's a valid number and unit (C, F, or K).
- Uses the
convert_temperaturefunction to perform the conversions and display the results.
Requirements
- Use appropriate function parameters and return values.
- Handle invalid inputs (non-numeric values or incorrect units) with error messages.
- Round all output temperatures to 2 decimal places.
- Include a main function to organize the program flow.
Conversion Formulas
- Celsius to Fahrenheit: °F = (°C × 9/5) + 32
- Celsius to Kelvin: K = °C + 273.15
- Fahrenheit to Celsius: °C = (°F - 32) × 5/9
- Kelvin to Celsius: °C = K - 273.15
Example Output
Enter a temperature value: 25
Enter the unit (C, F, or K): C
25.00°C equals:
77.00°F
298.15K
Tips
- Use
try/exceptto handle invalid numeric inputs. - Check if the unit is one of 'C', 'F', or 'K' (case-insensitive).
- Structure your program with a
main()function that calls other functions. - Test your program with different inputs, including edge cases like negative temperatures or invalid units.
Challenge
Add an optional feature to allow the user to continue converting temperatures until they choose to quit.