Search code examples
pythonflaskjinja2

Getting ZeroDivisionError when NOT dividing by zero


I have a flask app that returns a ZeroDivisionError. The numerator is zero but the denominator is not and the operation should return zero.

Using the Jinja2 debugger, I can query the variables to see that no division by zero is actually happening. See screenshot:

Does anybody know why this is happening?


Solution

  • Please remember about operator precedence. Your calculation seems to do 0 / 0 + 1529.657.... Division 0 / 0 is being done first, and so your operation results in ZeroDivisionError

    To solve your problem, put everything behind division sign in parentheses, to make it 0 / (0 + 1529.657...)