Convert Celsius to Fahrenheit
00:00
Next, start creating the convert_cel_to_far()
function.
00:06
convert_cel_to_far()
takes one argument that you need to define as a parameter, and this one is the Celsius temperature that you want to convert.
00:16
Name this variable temp_cel
. And then for the formula of temp_far
in the next line in the function body, you can use the formula that is stated in the challenge text, C * 9/5 + 32
.
00:35
And the C
in this case is the temp_cel
variable. So here now you can be very strict with Python formatting and put a space between the slash of 9 / 5
or even put parentheses surrounding it. For the calculation, it doesn’t matter, so here it’s more stylistic if you find it a bit better to read than how it was in the challenge text.
01:03
And then in the function, you need to return the temp_far
variable once you save and run the module and then call the convert_cel_to_far()
with the value 37
.
01:19 And then you should get the Fahrenheit temperature. And this one looks a bit weird. It is correct, it’s 98.6, but there are a bunch of zeros and the one. Let’s investigate our calculation.
01:32 What is going on there and why those zeros are appearing?
01:37 Remember that I said just a few moments ago that the parentheses are purely aesthetic. Apparently, they are not just purely aesthetic, but it also defines in which order Python calculates the values.
01:51 And depending on the calculation, the conversion of integer to floats gives a result that is not entirely representable in the other format.
02:05 That may be a bit more specific here. When you divide two integers in Python, the result can be a floating-point number. The representation of this number can sometimes include many zeros after the decimal point due to how floating-point arithmetic works in computers because computers use the binary system, which is a base-2 system, to represent numbers, while we humans—and I assume you are a human watching this—typically use the decimal, which is a base-10 system.
02:37 So when converting numbers between those two systems, some numbers that are easily represented in decimal cannot be precisely represented in binary. As a result, the binary representation can sometimes be an approximation, and that’s why you see the bunch of zeros and the one in this result, depending on if you put the parentheses in there or not.
03:03 If you want to learn more about the floating-point number representation, check out the Python Basics course on numbers and math, where Bartosz explains this topic way better than I did right now.
03:16 Now the cool thing in programming and your working in IDLE is that you can actually try it out, how it behaves differently when you remove the parentheses.
03:24
So if you take away the parentheses and make exactly the same function call with 37
, then you get 98.6
without the zeros and the one because the order of how Python executes this formula is different.
03:40
And again, you can try this out here in the IDLE shell. So let’s dissect this formula in the shell and re-create it bit by bit. First, we pass in 37
, and then it’s multiplied by 9
, which is 333
.
03:57
Then you divide it by 5
, which is 66.6
. So here, this conversion works without this approximation. And then you add the 32
to the 66.6
.
04:10
So this gives this clean 98.6
number. However, if you multiply the 37
by 9
divided by 5
—that’s when you put it into parentheses—then you get 66.6
and a bunch of zeros and a one.
04:30
So here it doesn’t entirely work. So let’s see what 9
divided by 5
is, which is 1.8
. And apparently, this one here is the culprit because when you multiply 37
by 1.8
, then the conversion isn’t clean anymore, and Python needs to do an approximation.
04:50
And this results in the 66.6
with a bunch of zeros and the one. So if you would add 32
here, then it would be the 98.6
with a bunch of zeros and the one.
05:02 So although it’s not exactly the same, you can choose whichever format because if you remember, you should round the number anyway to two digits after the decimal point.
05:14 So you don’t need to care that much about this approximation here. And I like the look with the parentheses more. So I will add the parentheses again and continue with this formula in the next lesson.
Become a Member to join the conversation.