Help with basic code issue

I’m in the workspace for the Python control flow assignment.
I added the following code to return the amount needed to lower BMI in order to be in the healthy range. I added "print(bmi_value) to double check that the correct number was being entered as an argument in the function.

elif (bmi_value >= 25) and (bmi_value <= 30):
newloss = (bmi_value - 24.9)
print(bmi_value)
print("Your BMI is in the overweight range. To lower your cost, you should lower your BMI by " + str(newloss) + “.”)

the return contains

26.2
Your BMI is in the overweight range. To lower your cost, you should lower your BMI by 1.3000000000000007.

So… What is wrong with my newloss variable definition? Why is 26.2 - 24.9 not returning 1.3, but instead 1.300000000000(ish)7?

I think this article will answer your question.

2 Likes

Thanks, makes sense. So, simple solution is always to use round in variable definition, like:
newloss = round(bmi_value - 24.9, 1)? Or is there a better way?

1 Like

No, that would be quite tedious.

You generally don’t need to worry about this. Just knowing how floating point numbers are represented by a computer and why Python displays the result it’s displaying is what’s important.

With this, we know that the result you got is correct.

EDIT: Unless you’re looking to display the result that you got back nice and neatly, I suppose. Then, yes, you would use the round() function to display the result of your function properly.

1 Like