Drone Shipping = 100.80000000000001

weight = 8.4 print("DRONE SHIPPING") if weight <= 2: cost_drone = weight * 4.5 elif weight <= 6: cost_drone = weight * 9 elif weight <= 10: cost_drone = weight * 12.00 else: cost_drone = weight * 14.25 print(cost_drone)

Why is this outputting 100.80000000000001 instead of just 100.8 ?

Here is a link to the lesson: https://www.codecademy.com/paths/build-chatbots-with-python/tracks/introduction-to-python-and-chatbots/modules/learn-python3-control-flow/projects/python-sals-shipping

It is the nature of floating point arithmetic to produce a minuscule error due to how binary math is performed. Decimal fractions need to be resolved from a binary mantissa and exponent. There is no absolute precision in this process.

The simplest way to resolve it is with print formatting.

print (f"{my_currency_value:.2f}")

Now it will print as 108.80.

Note above that we do not need to recast the value to str. The format engine treats it as a float given the directive we supply after the colon.

.2f  =>  2 decimal places

We are not mutating the value, only representing it in string form.

1 Like

Thanks for the fix. It’s going to take me a while to understand what you mean, there’s several words in your reply I’ve never heard before.

Should this be reported as a bug to the person who wrote the lesson?

No, it’s not a bug. Normal behavior in any language.

When you have the time, google ‘floating point arithmetic’ to get a look at the mechanics of how floats are stored.