Why do I get a result with so many digits when I multiply two numbers?

weight = 8.4

if weight <= 2:
  cost_of_drone_shipping = 4.5 * weight
  print(cost_of_drone_shipping)
elif weight > 2 and weight <= 6:
  cost_of_drone_shipping = 9 * weight
  print(cost_of_drone_shipping)
elif weight > 6 and weight <= 10:
  cost_of_drone_shipping = 12 * weight
  print(cost_of_drone_shipping)
elif weight > 10:
  cost_of_drone_shipping = 14.25 * weight
  print(cost_of_drone_shipping)
else:
  print("Please enter correct weight number")

# Result is 100.80000000000001

For the code above, the result should be 100.8. I dont know why the python gave me this endless number. Thank you for helping me

1 Like

Hi there.

It’s a consequence of the inherent inaccuracy of floating point arithmetic.

The Python docs have a pretty decent explanation of it. :slight_smile:

4 Likes