我正在创建一个系统,使用 python 3 对用户进行集成和区分测试。当我显示问题时,它们的形式如下:
-25*x**(3/5)/3 + 6*x**(4/3) - 5*x**6/3 + x**2/2 - 4*x
我怎样才能把它改成这样的形式:
-25x^(3/5)/3 + 6x^(4/3) - 5x^6/3 + x^2/2 - 4x
我也想要它,这样当用户输入等效答案时它仍然可以被识别
最佳答案
对于简单的显示器更换,您可以使用:
def format_math(string):
return (string.replace("**", "^")).replace("*", "")
然后您可以将其与用户输入进行比较,以将他们的输入答案与您的答案进行比较。
x = format_math("-25*x**(3/5)/3 + 6*x**(4/3) - 5*x**6/3 + x**2/2 - 4*x")
# -25x^(3/5)/3 + 6x^(4/3) - 5x^6/3 + x^2/2 - 4x
user_input = format_math(input("Enter your answer: "))
# If the user enters # -25x^(3/5)/3 + 6x^(4/3) - 5x^6/3 + x^2/2 - 4x or
# -25*x**(3/5)/3 + 6*x**(4/3) - 5*x**6/3 + x**2/2 - 4*x the program will
# recognize both as correct
if x == user_input:
return True
关于python - 与 sympy/pyqt python 的集成和区分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28682283/