55 lines
2.0 KiB
Markdown
55 lines
2.0 KiB
Markdown
---
|
||
title: Temperature Converter
|
||
description:
|
||
published: true
|
||
date: 2025-06-28T01:36:15.092Z
|
||
tags:
|
||
editor: markdown
|
||
dateCreated: 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:
|
||
1. Defines three functions to handle temperature conversions:
|
||
- `celsius_to_fahrenheit(celsius)`: Converts Celsius to Fahrenheit
|
||
- `celsius_to_kelvin(celsius)`: Converts Celsius to Kelvin
|
||
- `convert_temperature(value, unit)`: Takes a temperature value and unit (C, F, or K) and returns conversions to the other two units
|
||
2. Prompts the user to input a temperature value and its unit.
|
||
3. Validates the input to ensure it's a valid number and unit (C, F, or K).
|
||
4. Uses the `convert_temperature` function 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`/`except` to 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.
|