stringExp = "2^4"
intVal = int(stringExp) # Expected value: 16
This returns the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int()
with base 10: '2^4'
I know that eval
can work around this, but isn't there a better and - more importantly - safer method to evaluate a mathematical expression that is being stored in a string?