Eval/Exec with assigning variable - Python
问题 The below piece of code is aimed at converting a factorial into its products. E.g. "4!" --> "(4*3*2*1)" . This code does not work due to the line exec(codeToRun) . However, if I instead put the value of codeToRun in place of exec(codeToRun) then it works perfectly so why doesn't exec work? Doesn't work ↓ def checkSpecialChars(char, stringToCheck, codeToRun): while char in stringToCheck: currentString="" for i in range(len(stringToCheck)): if stringToCheck[i]==char: try: eval(codeToRun) except