Ask coding questions

← Back to all posts
Why is the Python math not working
PYTHORE3605 (103)

In lemon.py in line 54, the math doesn't work if you do it more then one time. To see this, run the program, chose lemonade stand, and try to buy sugar more then one time, and the math messes up. I should have 8.85 but you get 8.8500000000000005. How do I fix?

Answered by Coder100 (16757) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (16757)

That is because python is not good at math duh

fun fact: computers cant actually do decimals, this is one of them.

Here's how you can solve it:

def add_accurate(a, b):
  return (a * 100000 + b * 100000) / 100000
Muffinlavania (1510)

You can just take your money and do cash=round(cash), sometimes the math just gets bit funny.

AloegelhiPlaysR (104)

Using the math.floor() method.

CookieSnowOwl (40)

do minus 0.5 instead of -0.50 i think it may work... idk