|
| 1 | +# Chapter 10: Temperature Converter Program |
| 2 | + |
| 3 | +## 📚 What You'll Learn |
| 4 | +Convert temperatures between Fahrenheit and Celsius - essential for international travel and science! |
| 5 | + |
| 6 | +## 🎯 Learning Objectives |
| 7 | +- Understand temperature scales (Fahrenheit and Celsius) |
| 8 | +- Apply mathematical conversion formulas |
| 9 | +- Work with floating-point arithmetic |
| 10 | +- Create conversion programs |
| 11 | +- Format temperature output properly |
| 12 | + |
| 13 | +## 📖 Concept Explanation |
| 14 | + |
| 15 | +### Temperature Scales |
| 16 | + |
| 17 | +#### Fahrenheit (°F) |
| 18 | +- Used in the United States |
| 19 | +- Water freezes at 32°F |
| 20 | +- Water boils at 212°F |
| 21 | +- Normal body temperature: 98.6°F |
| 22 | + |
| 23 | +#### Celsius (°C) |
| 24 | +- Used in most of the world |
| 25 | +- Water freezes at 0°C |
| 26 | +- Water boils at 100°C |
| 27 | +- Normal body temperature: 37°C |
| 28 | + |
| 29 | +### Conversion Formulas |
| 30 | + |
| 31 | +#### Celsius to Fahrenheit |
| 32 | +``` |
| 33 | +F = (C × 9/5) + 32 |
| 34 | +or |
| 35 | +F = (C × 1.8) + 32 |
| 36 | +``` |
| 37 | + |
| 38 | +#### Fahrenheit to Celsius |
| 39 | +``` |
| 40 | +C = (F - 32) × 5/9 |
| 41 | +or |
| 42 | +C = (F - 32) / 1.8 |
| 43 | +``` |
| 44 | + |
| 45 | +## 💡 Examples |
| 46 | + |
| 47 | +### Example 1: Freezing Point |
| 48 | +```python |
| 49 | +celsius = 0 |
| 50 | +fahrenheit = (celsius * 9/5) + 32 |
| 51 | +print(f"{celsius}°C = {fahrenheit}°F") # 0°C = 32°F |
| 52 | +``` |
| 53 | + |
| 54 | +### Example 2: Boiling Point |
| 55 | +```python |
| 56 | +celsius = 100 |
| 57 | +fahrenheit = (celsius * 9/5) + 32 |
| 58 | +print(f"{celsius}°C = {fahrenheit}°F") # 100°C = 212°F |
| 59 | +``` |
| 60 | + |
| 61 | +### Example 3: Body Temperature |
| 62 | +```python |
| 63 | +fahrenheit = 98.6 |
| 64 | +celsius = (fahrenheit - 32) * 5/9 |
| 65 | +print(f"{fahrenheit}°F = {celsius:.1f}°C") # 98.6°F = 37.0°C |
| 66 | +``` |
| 67 | + |
| 68 | +## ✍️ Practice Exercises |
| 69 | + |
| 70 | +### Exercise 1: Add Kelvin Conversion |
| 71 | +```python |
| 72 | +# Kelvin to Celsius: K - 273.15 = C |
| 73 | +# Celsius to Kelvin: C + 273.15 = K |
| 74 | +``` |
| 75 | + |
| 76 | +### Exercise 2: Improve User Experience |
| 77 | +```python |
| 78 | +print("=== Temperature Converter ===") |
| 79 | +print("F - Convert from Fahrenheit to Celsius") |
| 80 | +print("C - Convert from Celsius to Fahrenheit") |
| 81 | +``` |
| 82 | + |
| 83 | +### Exercise 3: Add Temperature Categories |
| 84 | +```python |
| 85 | +if temp_celsius < 0: |
| 86 | + print("Below freezing!") |
| 87 | +elif temp_celsius < 10: |
| 88 | + print("Cold") |
| 89 | +elif temp_celsius < 20: |
| 90 | + print("Cool") |
| 91 | +elif temp_celsius < 30: |
| 92 | + print("Warm") |
| 93 | +else: |
| 94 | + print("Hot!") |
| 95 | +``` |
| 96 | + |
| 97 | +### Exercise 4: Batch Conversion |
| 98 | +```python |
| 99 | +# Convert multiple temperatures at once |
| 100 | +temps = [0, 32, 68, 100] |
| 101 | +for temp in temps: |
| 102 | + celsius = (temp - 32) * 5/9 |
| 103 | + print(f"{temp}°F = {celsius:.1f}°C") |
| 104 | +``` |
| 105 | + |
| 106 | +## 📝 Common Temperature Conversions |
| 107 | + |
| 108 | +| Celsius (°C) | Fahrenheit (°F) | Description | |
| 109 | +|--------------|-----------------|-------------| |
| 110 | +| -40 | -40 | Same in both scales! | |
| 111 | +| -18 | 0 | Very cold | |
| 112 | +| 0 | 32 | Water freezes | |
| 113 | +| 10 | 50 | Cool | |
| 114 | +| 20 | 68 | Room temperature | |
| 115 | +| 37 | 98.6 | Body temperature | |
| 116 | +| 100 | 212 | Water boils | |
| 117 | + |
| 118 | +## 🎮 Enhanced Temperature Converter |
| 119 | + |
| 120 | +```python |
| 121 | +def temperature_converter(): |
| 122 | + """Convert between Fahrenheit and Celsius""" |
| 123 | + |
| 124 | + print("=" * 45) |
| 125 | + print(" TEMPERATURE CONVERTER") |
| 126 | + print("=" * 45) |
| 127 | + |
| 128 | + # Get input unit |
| 129 | + print("\nWhat unit is your temperature in?") |
| 130 | + print("F - Fahrenheit") |
| 131 | + print("C - Celsius") |
| 132 | + unit = input("Enter F or C: ").upper() |
| 133 | + |
| 134 | + # Get temperature value |
| 135 | + try: |
| 136 | + temp = float(input("Enter temperature: ")) |
| 137 | + except ValueError: |
| 138 | + print("Error: Please enter a valid number!") |
| 139 | + return |
| 140 | + |
| 141 | + # Convert and display |
| 142 | + if unit == "F": |
| 143 | + celsius = (temp - 32) * 5/9 |
| 144 | + print(f"\n{temp}°F = {celsius:.2f}°C") |
| 145 | + |
| 146 | + # Add category |
| 147 | + if celsius < 0: |
| 148 | + print("Status: Freezing!") |
| 149 | + elif celsius < 20: |
| 150 | + print("Status: Cold") |
| 151 | + elif celsius < 30: |
| 152 | + print("Status: Warm") |
| 153 | + else: |
| 154 | + print("Status: Hot!") |
| 155 | + |
| 156 | + elif unit == "C": |
| 157 | + fahrenheit = (temp * 9/5) + 32 |
| 158 | + print(f"\n{temp}°C = {fahrenheit:.2f}°F") |
| 159 | + |
| 160 | + # Add category |
| 161 | + if temp < 0: |
| 162 | + print("Status: Freezing!") |
| 163 | + elif temp < 20: |
| 164 | + print("Status: Cold") |
| 165 | + elif temp < 30: |
| 166 | + print("Status: Warm") |
| 167 | + else: |
| 168 | + print("Status: Hot!") |
| 169 | + else: |
| 170 | + print("Error: Invalid unit! Please enter F or C") |
| 171 | + |
| 172 | + print("=" * 45) |
| 173 | + |
| 174 | +# Run converter |
| 175 | +temperature_converter() |
| 176 | +``` |
| 177 | + |
| 178 | +## 🌡️ Real-World Applications |
| 179 | + |
| 180 | +### Weather Reporting |
| 181 | +```python |
| 182 | +def weather_report(temp_c): |
| 183 | + """Display weather with temperature""" |
| 184 | + temp_f = (temp_c * 9/5) + 32 |
| 185 | + |
| 186 | + print(f"\nToday's Temperature:") |
| 187 | + print(f" {temp_c:.1f}°C / {temp_f:.1f}°F") |
| 188 | + |
| 189 | + if temp_c < 0: |
| 190 | + print(" 🥶 Freezing! Bundle up!") |
| 191 | + elif temp_c < 15: |
| 192 | + print(" 🧥 Chilly. Bring a jacket!") |
| 193 | + elif temp_c < 25: |
| 194 | + print(" 😊 Pleasant weather!") |
| 195 | + else: |
| 196 | + print(" ☀️ Hot! Stay hydrated!") |
| 197 | + |
| 198 | +weather_report(22) |
| 199 | +``` |
| 200 | + |
| 201 | +### Cooking/Baking |
| 202 | +```python |
| 203 | +def oven_temp_converter(celsius): |
| 204 | + """Convert baking temperatures""" |
| 205 | + fahrenheit = (celsius * 9/5) + 32 |
| 206 | + |
| 207 | + print(f"\nOven Temperature:") |
| 208 | + print(f" {celsius}°C = {fahrenheit:.0f}°F") |
| 209 | + |
| 210 | + # Gas mark (UK) |
| 211 | + if 140 <= celsius <= 150: |
| 212 | + print(" Gas Mark: 1") |
| 213 | + elif 160 <= celsius <= 180: |
| 214 | + print(" Gas Mark: 4") |
| 215 | + elif 190 <= celsius <= 200: |
| 216 | + print(" Gas Mark: 6") |
| 217 | + elif 220 <= celsius <= 230: |
| 218 | + print(" Gas Mark: 8") |
| 219 | + |
| 220 | +oven_temp_converter(180) |
| 221 | +``` |
| 222 | + |
| 223 | +## 🚀 Challenge Projects |
| 224 | + |
| 225 | +### Challenge 1: Triple Converter |
| 226 | +Add Kelvin scale: |
| 227 | +```python |
| 228 | +# Celsius to Kelvin: K = C + 273.15 |
| 229 | +# Kelvin to Celsius: C = K - 273.15 |
| 230 | +# Absolute zero: 0 K = -273.15°C = -459.67°F |
| 231 | +``` |
| 232 | + |
| 233 | +### Challenge 2: Temperature Range Converter |
| 234 | +```python |
| 235 | +# Convert a range of temperatures |
| 236 | +low_f = 50 |
| 237 | +high_f = 80 |
| 238 | + |
| 239 | +low_c = (low_f - 32) * 5/9 |
| 240 | +high_c = (high_f - 32) * 5/9 |
| 241 | + |
| 242 | +print(f"Range: {low_f}-{high_f}°F = {low_c:.0f}-{high_c:.0f}°C") |
| 243 | +``` |
| 244 | + |
| 245 | +### Challenge 3: Scientific Temperature Converter |
| 246 | +```python |
| 247 | +def convert_temperature(value, from_unit, to_unit): |
| 248 | + """Convert between C, F, and K""" |
| 249 | + # Convert to Celsius first |
| 250 | + if from_unit == 'F': |
| 251 | + celsius = (value - 32) * 5/9 |
| 252 | + elif from_unit == 'K': |
| 253 | + celsius = value - 273.15 |
| 254 | + else: |
| 255 | + celsius = value |
| 256 | + |
| 257 | + # Convert from Celsius to target unit |
| 258 | + if to_unit == 'F': |
| 259 | + return (celsius * 9/5) + 32 |
| 260 | + elif to_unit == 'K': |
| 261 | + return celsius + 273.15 |
| 262 | + else: |
| 263 | + return celsius |
| 264 | + |
| 265 | +print(convert_temperature(100, 'C', 'F')) # 212°F |
| 266 | +print(convert_temperature(32, 'F', 'K')) # 273.15 K |
| 267 | +``` |
| 268 | + |
| 269 | +## 🔍 Understanding the Math |
| 270 | + |
| 271 | +### Why 5/9 and 9/5? |
| 272 | +- Celsius: 100 degrees between freezing and boiling |
| 273 | +- Fahrenheit: 180 degrees between freezing and boiling |
| 274 | +- Ratio: 180/100 = 9/5 or 100/180 = 5/9 |
| 275 | + |
| 276 | +### Why add/subtract 32? |
| 277 | +- Water freezes at 0°C but 32°F |
| 278 | +- This 32-degree offset must be adjusted |
| 279 | + |
| 280 | +## 📊 Conversion Table Generator |
| 281 | +```python |
| 282 | +print("°C | °F") |
| 283 | +print("-" * 15) |
| 284 | +for celsius in range(-10, 41, 5): |
| 285 | + fahrenheit = (celsius * 9/5) + 32 |
| 286 | + print(f"{celsius:3d} | {fahrenheit:5.1f}") |
| 287 | +``` |
| 288 | + |
| 289 | +Output: |
| 290 | +``` |
| 291 | +°C | °F |
| 292 | +--------------- |
| 293 | +-10 | 14.0 |
| 294 | + -5 | 23.0 |
| 295 | + 0 | 32.0 |
| 296 | + 5 | 41.0 |
| 297 | + 10 | 50.0 |
| 298 | + 15 | 59.0 |
| 299 | + 20 | 68.0 |
| 300 | + 25 | 77.0 |
| 301 | + 30 | 86.0 |
| 302 | + 35 | 95.0 |
| 303 | + 40 | 104.0 |
| 304 | +``` |
| 305 | + |
| 306 | +## 🔗 Next Chapter |
| 307 | +Continue to [Chapter 11: Logical Operators](../11-logical/) to learn about AND, OR, and NOT operations! |
0 commit comments