from math import *
def solution_handler(self, input):
while re.search(r'(?<!\.)(\d+)(?!\.)(\d*)', input):
rx = re.search(r'(?<!\.)(\d+)(?!\.)(\d*)', input)
input = input[:rx.start()] + rx.group() + ".0" + input[rx.end():]
exec "solution = " + input
return solution
This is the code that I’m using to solve the equations entered into the calculator I’m working on. It seems to work fine most of the time, but if I try to enter a function (cos, sin, etc.) with a value outside of [-9,9], the program freezes.
What did I do wrong?
Example strings:
exec "solution = " + "6*cos(6)" -> solution = 5.761 ...exec "solution = " + "7/cos(8/2)" -> solution = -10.709 ...exec "solution = " + "sin(12) + 3" -> Freezeexec "solution = " + "abs(-50) / 2" -> Freeze
It seems to be the case with any function that I try to use.
The problem is with your loop: remove the
execand it would still hang. Try this instead: