Python 如果x不在[-9,9]中,exec cos(x)将冻结程序 从数学导入* def溶液处理程序(自身,输入): 在重新搜索(r’(?

Python 如果x不在[-9,9]中,exec cos(x)将冻结程序 从数学导入* def溶液处理程序(自身,输入): 在重新搜索(r’(?,python,regex,exec,Python,Regex,Exec,时,问题在于循环:删除exec,它仍将挂起。请尝试以下操作: from math import * def solution_handler(self, input):

时,问题在于循环:删除
exec
,它仍将挂起。请尝试以下操作:

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

你能显示当它失败时传递给
exec
的字符串吗?使用
sympy
模块而不是
exec
。那么,这里有什么可能冻结?显然
while
循环在有问题的输入上变成了一个无限循环。你必须弄清楚为什么;-)这正是我想要的。显然,你知道循环应该找到所有整数并将它们转换为浮点数,但我从未想过要导入未来。:)干杯!
from __future__ import division
from math import *
def solution_handler(self, input):
    return eval(input)