I’m trying to do something that I thought would be really easy, but my attempt is turning into a code hairball. I must be missing something.
I have a series of strings that represent math sequences. Sometimes, the string has invalid characters, so I would leave the string as is. But if the string contains valid math, I would like to show the result.
For example, if I had '(18) + 5’, I would like to display 13, and if I literally type (18) + 5 in the script playground, I get 13. But if the string is ‘(1*temp)+5’, I don’t want to perform the math, and return the string instead.
In my first attempt, I split the string and ran each char through a try/except to determine if there were any non-numeric values (the numbers could be ints or floats). When I found a non-number, non-para, or non-operator, I broke from the loop and left it as a string. Seems like a hokey way to do it, but it worked.
But, what do I do even if I can determine that the string represents valid math? Do I then have to write another sequence that evaluates each operation and perform each part of the equation seperately? I never know in advance what the math string will look like, so I might have something like this- ‘((1*6)+7)+32+4)’. Although it would compute if I typed that directly into the playground, how would I do this in script?
I don’t need someone to write the code for me, but if there is some type-conversion or math libraries that I don’t know about that would help, I’d like to hear about them.