Python Coding Exercise #17: Temperatures
Part of the series Python Practice Exercise — part 17
Before you start: Know input() and int() · Know if statements and f-strings
The question
Please write a program which asks the user for a temperature in degrees Fahrenheit, and then prints out the same in degrees Celsius. If the converted temperature falls below zero degrees Celsius, the program should also print out “Brr! It’s cold in here!”.
The formula for converting degrees Fahrenheit to degrees Celsius can be found easily by any search engine of your choice.
Two examples of expected behaviour:
Please type in a temperature (F): 101
101 degrees Fahrenheit equals 38.333333333333336 degrees Celsius
Please type in a temperature (F): 21
21 degrees Fahrenheit equals -6.111111111111111 degrees Celsius
Brr! It's cold in here!
My Solution
temperature = int(input("Please type in a temperature (F): "))
temp_in_celsius = (temperature-32)/1.8
if temp_in_celsius >= 0:
print(f"{temperature} degrees Fahrenheit equals {temp_in_celsius} degrees Celsius")
if temp_in_celsius < 0:
print(f"{temperature} degrees Fahrenheit equals {temp_in_celsius} degrees Celsius\nBrr! It's cold in here!")
How My Solution Works
1. The conversion formula
To convert Fahrenheit to Celsius, subtract 32 and then divide by 1.8:
Celsius = (Fahrenheit - 32) / 1.8
For 101°F, that’s (101 - 32) / 1.8 = 69 / 1.8 = 38.333.... The / operator returns a float, which is why the result has a long decimal.
The brackets around temperature-32 matter. Without them, Python would divide 32 by 1.8 first (division comes before subtraction) and give a very different answer.
2. ifstatements
The two conditions are opposites:
temp_in_celsius >= 0is true for zero and above.temp_in_celsius < 0is true for everything below zero.
Exactly one of them is true for any temperature, so exactly one message is printed. Note that 0°C is not “cold” here. At 32°F the result is 0.0, which is >= 0, so no “Brr!” appears.
3. The cold branch prints two lines
In the second if, the \n inside the f-string starts a new line. One print() therefore outputs the conversion line and Brr! It's cold in here! underneath it. (More on \n in Data Types in Python.)
University Solution
Look at the two print() lines in My Solution. The first part of the message, ... degrees Fahrenheit equals ... degrees Celsius, is written twice, once in each if. The University Solution avoids that:
temperature_f = int(input("Please type in a temperature (F): "))
temperature_c = (temperature_f - 32) * 5/9
print(f"{temperature_f} degrees Fahrenheit equals {temperature_c} degrees Celsius")
if temperature_c < 0:
print("Brr! It's cold in here!")
The conversion line is printed for everyone, because it sits outside any if. Then a single if adds the extra message only when the temperature is below zero. The output is exactly the same, but each piece of text is written once, so it’s easier to read and to change.
This version also uses clearer variable names. temperature_f and temperature_c show at a glance which scale each one holds.
Two formulas, slightly different digits
The University Solution uses (F - 32) * 5/9 while My Solution uses (F - 32) / 1.8. In maths these are identical, because 5/9 and 1/1.8 are the same number. But floating-point numbers are stored with tiny rounding differences, so the two can disagree in the very last digit:
print((-60 - 32) / 1.8) # -51.11111111111111
print((-60 - 32) * 5 / 9) # -51.111111111111114
For the two examples in the exercise, 101 and 21, both formulas give exactly the digits shown in the sample output. But for some other temperatures, they don’t. When an exercise expects an exact printout, it’s worth using the formula the course uses. And whenever you see numbers like 51.111111111111114, remember that it’s floating-point rounding, not a bug in your program.
Key takeaways
- The conversion is
Celsius = (Fahrenheit - 32) / 1.8, or equivalently(Fahrenheit - 32) * 5/9. - Use brackets so the subtraction happens before the division.
>= 0and< 0are opposites, so exactly one of them is true. Zero counts as not cold.- If a message appears in every case, print it once outside the
if, then use aniffor the extra part. - Two formulas that are equal in maths can differ in the last digit of a float.
✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦
Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.