Study Guide

6.4.9 Temperature Converter CodeHS Answers

6.4.9 Temperature Converter CodeHS Answers

The most common answer is:

Write your function for converting Celsius to Fahrenheit here.
Make sure to include a comment at the top that says what
each function does!
def to_F(c):
return float(1.8 * c + 32)
Now write your function for converting Fahrenheit to Celsius.
def to_C(f):
return float((f – 32) / 1.8)
Now change 0C to F:
print (to_F(0))
Now change 100C to F:
print (to_F(100))
Now change 40C to F:
print (to_C(40))
Now change 80C to F:
print (to_C(80))

Here’s the corrected and commented version of the temperature conversion program:

# Converts Celsius to Fahrenheit
def to_F(c):
    return float(1.8 * c + 32)

# Converts Fahrenheit to Celsius
def to_C(f):
    return float((f - 32) / 1.8)

# Change 0°C to °F
print(to_F(0))  # Expected output: 32°F

# Change 100°C to °F
print(to_F(100))  # Expected output: 212°F

# Corrected: Now change 40°F to °C
print(to_C(40))  # Expected output: Approximately 4.444°C

# Corrected: Now change 80°F to °C
print(to_C(80))  # Expected output: Approximately 26.667°C

This program:

  • Defines a function to_F(c) that converts Celsius to Fahrenheit.
  • Defines a function to_C(f) that converts Fahrenheit to Celsius.
  • Prints the conversion results for given temperatures, first converting from Celsius to Fahrenheit and then converting from Fahrenheit to Celsius with the correct function calls.