0

I have a string like 8/c123*c456 where c123 and c456 are variables containing a float. I use Regex to separate the string into a list ['8', '/', 'c123', '*', 'c456']. I replace 'c123' and 'c456' with a string corresponding to their value (like '4.0' for example). I then concatenate the string and try to evaluate it using the class NumericStringParser from this question (Evaluating a mathematical expression in a string).

The problem is when the value of one of the variables is 0 and I end up trying to divide by 0 in the operation. I would like that operation to return 0.

I saw the answers from this question Make division by zero equal to zero but I don't know how I can use them.

sol
  • 83
  • 5

1 Answers1

2

you can try

try:
  #Your operation here
except ZeroDivisionError:
  # return 0 here 

More information here: https://en.wikibooks.org/wiki/Python_Programming/Exceptions